从函数中修改列表
一家为用户提交的设计制作3D打印模型的公司,需要打印的设计存储在一个列表中,打印后移到另一个列表中。
#!/usr/bin/env python # -*- coding:utf-8 -*- unprinted_designs = ['iphone case','robot pendant','dodecahedron'] completed_models = [] while unprinted_designs: current_design = unprinted_designs.pop() print("Printing model:" + current_design) completed_models.append(current_design) print(" The following models have been printed:") for completed_model in completed_models: print(completed_model)
#!/usr/bin/env python # --*-- encoding:utf-8 --*-- def print_models(unprinted_designs,completed_models): while unprinted_designs: current_design = unprinted_designs.pop() 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 = ['iphone case','robot pendant','dodecahedron'] completed_models = [] print_models(unprinted_designs,completed_models) show_completed_models(completed_models)
采用描述性的函数名;在一个函数中,总是可以调用另一个函数
禁止函数修改列表
切片表示法[:]创建列表的副本,如果不想清空未打印的设计列表,可以这样调用print_models();
print_models(unprinted_designs[:], completed_models)
#!/usr/bin/env python # --*-- encoding:utf-8 --*-- def print_models(unprinted_designs,completed_models): while unprinted_designs: current_design = unprinted_designs.pop() 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 = ['iphone case','robot pendant','dodecahedron'] completed_models = [] print_models(unprinted_designs[0:2],completed_models) show_completed_models(completed_models)
传递任意数量的实参
#!/usr/bin/env python
# --*-- encoding:utf-8 --*--
def make_pizza(*toppings):
print(toppings)
make_pizza('peppersoni')
make_pizza('mushrooms','green peppers','extra cheese')
('peppersoni',) ('mushrooms', 'green peppers', 'extra cheese')
形参名 *toppings中的星号,让Python创建一个名为toppings的空元组,并将收到的所有值都封装在元组中。
#!/usr/bin/env python
# --*-- encoding:utf-8 --*--
def make_pizza(*toppings):
print(" Making a pizza with the following toppings:")
for topping in toppings:
print("- " + topping)
make_pizza('peppersoni')
make_pizza('mushrooms','green peppers','extra cheese')
Making a pizza with the following toppings: - peppersoni Making a pizza with the following toppings: - mushrooms - green peppers - extra cheese
结合使用位置实参和任意数量实参
如果要让函数接收不同类型的实参,必须在函数定义中将接纳任意数量实参的形参放在最后。Python先匹配位置实参和关键字实参,再将余下的实参都收集到最后一个形参中。
#!/usr/bin/env python
# --*-- encoding:utf-8 --*--
def make_pizza(size,*toppings):
print(" Making a " + str(size) + "-inch pizza with the following toppings:")
for topping in toppings:
print("- " + topping)
make_pizza(16,'pepppersoni')
make_pizza(12,'mushrooms','green peppers','extra cheese')
Making a 16-inch pizza with the following toppings: - pepppersoni Making a 12-inch pizza with the following toppings: - mushrooms - green peppers - extra cheese
使用任意数量的关键字实参
有时候需要接收任意数量的实参,但预先不知道传递给函数的会是什么信息,这种情况下,可将函数编写成能够接收任意数量的键值对
#!/usr/bin/env python
# --*-- encoding:utf-8 --*--
def build_profile(first,last,**user_info):
profile = {}
profile['first_name'] = first
profile['last_name'] = last
for key,value in user_info.items():
profile[key] = value
return profile
user_profile = build_profile('albert','dinstein',
location = 'princeton',
field = 'physics')
print(user_profile)
将函数存储在模块中
1:导入整个模块
pizza.py
#!/usr/bin/env python
# --*-- encoding:utf-8 --*--
def make_pizza(size,*toppings):
print("
Making a " + str(size) +
"-inch pizza with the following toppings:")
for topping in toppings:
print("- " + topping)
在pizza.py目录下创建making_pizzas.py
#!/usr/bin/env python
# --*-- encoding:utf-8 --*--
import pizza
pizza.make_pizza(16,'pepperoni')
pizza.make_pizza(12,'mushrooms','green peppers','extra cheese')
Making a 16-inch pizza with the following toppings: - pepperoni Making a 12-inch pizza with the following toppings: - mushrooms - green peppers - extra cheese
2:导入特定的函数:
from module_name import function_name
通过用逗号分隔函数名,可根据需要从模块中导入任意数量的函数:
from module_nmae import function_0,function_1,function_2
from pizza import make_pizza
make_pizza(16,'pepperoni')
若使用这种语法,调用函数时就无需使用句点。由于在import语句中显式地导入了函数make_pizza(),因此调用它是只需指定起名字
3:使用AS给函数指定别名
from module_name import function_name as fn
from pizza import make_pizza as mp
mp(16,'pepperoni')
4.使用as给模块指定别名
import pizza as p
p.make_pizza(16,'pepperoni')
5:导入模块中的所有函数
from module_name import *
import语句中的星号让Python将模块pizza中的每个函数都复制到这个程序文件中。由于导入了每个函数,可通过名称来调用每个函数,而无需使用句点表示法。但可能出现模块中的函数名与本项目中使用的名称相同。
最佳的做法是:妖魔只导入你需要使用的函数,要么导入整个模块并使用句点表示法。
函数编写指南
1:给函数,模块指定描述性名称,且只在其中使用小写字母和下划线
2:每个函数应包含简要地阐述其功能的注释,该注释莹紧跟在函数定义后面,并采用文档字符串格式。
3:给形参制定默认值,等号两边不要有空格:
def function_name(parameter_0,parameter_1='default value')
对于函数调用中的关键字实参,也可遵循这种约定:
function_name(value_0,parameter='value')