定义函数
#定义函数
def greet_user():
print("Hello~")
#调用函数
greet_user()
#向函数传递信息
def print_user(username):
print(f"Hello~{username}")
print_user('Jesse')
传递实参数
#位置实参
def describe_pet(animal_type, pet_name):
print(f"I have a {animal_type}")
print(f"My {animal_type}'s name is {pet_name.title()}")
describe_pet('hamster', 'harry')
#关键字实参数&默认值
def describe_peet(animal_type='haster', pet_name='harry'):
print(f"I have a {animal_type}")
print(f"My {animal_type}'s name is {pet_name.title()}")
describe_peet()
describe_peet(pet_name='Zhangsan', animal_type='human')
返回值
#返回简单值
def get_formatted_name(first_name, last_name):
full_name = f"{first_name} {last_name}"
return full_name.title()
musician = get_formatted_name('jimi', 'hendrix')
print(musician)
#让实参数可选
def formatted_name(first_name, last_name, middle_name=''):
full_name = f"{first_name} {middle_name} {last_name}"
return full_name.title()
musician = formatted_name('jimi', 'hendrix')
print(musician)
musician = formatted_name('jimi', 'hendrix', 'lee')
print(musician)
传递列表
#在函数中修改列表
def print_models(unprinted_designs, completed_models):
while unprinted_designs:
current_design = unprinted_designs.pop()
print(f"Printing model:{current_design}")
completed_models.append(current_design)
def show_completed_models(completed_models):
print("The following models have been printed:")
for completed_model in completed_models:
print(completed_model)
unprinted_designs = ['phone case', 'robot pendant', 'dodecahedron']
completed_models = []
print_models(unprinted_designs, completed_models)
show_completed_models(completed_models)
#禁止函数修改列表
print_models(unprinted_designs[:], completed_models)
传递任意数量的实参
#传递任意数量的实参数
def make_pizza(*toppings):
print(toppings)
make_pizza('pepperoni')
make_pizza('mushrooms', 'green peppers', 'extra cheese')
#结合使用位置实参和任意数量实参
def make_pizza_size(size, *toppings):
print(f"Making a {size} pizza with the following toppings:")
for top in toppings:
print(f"-{top}")
make_pizza_size(16, 'pepperoni')
make_pizza_size(12, 'mushrooms', 'green peppers', 'extra cheese')
#使用任意数量的关键字实参数
def build_profile(first, last, **user_info):
user_info['first_name'] = first
user_info['last_name'] = last
return user_info
user_info = build_profile('albert', 'einstein', location='princeton', field='physics')
print(user_info)
将函数存储在模块中
1、导入整个模块
pizza.py
def make_pizza(size, *toppings):
print(f"Making a {size} pizza with the following toppings:")
for top in toppings:
print(f"-{top}")
pizza_main.py
import pizza
pizza.make_pizza(16, 'pepperoni')
pizza.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
2、导入特定的函数
#导入单个函数
from module_name import function_name
#导入多个函数
from module_name import function_0, function_1
from pizza import make_pizza
make_pizza(16, 'pepperoni')
make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
3、使用 as 给函数指定别名
from pizza import make_pizza as mp
mp(16, 'pepperoni')
mp(12, 'mushrooms', 'green peppers', 'extra cheese')
4、使用 as 给模块指定别名
import pizza as p
p.make_pizza(16, 'pepperoni')
p.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
函数编写指南
给形参指定默认值时,等号两边不要有空格:
def function_name(parameter_0, parameter_1='default value')
对于函数调用中的关键实参,也遵循整个约定
function_name(value_0, parameter_1='value')
函数参数缩进对齐
def function_name(
parameter_0, parameter_1, parameter_2,
parameter_3, parameter_4, parameter_5):
使用空行分隔多个函数
所有import 语句都放在文件开头
|