1 禁止函数修改列表
办法将列表的副本传递给函数: list_name[:]
1、1主函数
封装成两个函数
1、打印模型函数
def print_models(unprinted_designs,completed_models):
"""
模拟打印每个设计,直到没有未打印的设计为止
打印每个设计后,都将其移到表completed_models中
"""
while unprinted_designs:
current_design = unprinted_designs.pop()
# 模拟根据设计制造3A打印模型的过程
print("Print model: " + current_design)
completed_models.append(current_design)
2、显示函数
def show_completed_models(completed_models):
"""显示打印好的所有模型"""
print("\nThe following models have been printed: ")
for completed_model in completed_models:
print(completed_model)
unprinted_designs = ['iphone case','robot pendant','dodecahedron']
completed_models = []
print("\nunprinted_designs未处理前结果",":",unprinted_designs)
print_models(unprinted_designs[:], completed_models)
将unprinted_designs[:]副本传给函数体
print("unprinted_designs处理后结果",":",unprinted_designs)
show_completed_models(completed_models)
结果:
unprinted_designs未处理前结果 : ['iphone case', 'robot pendant', 'dodecahedron']
Print model: dodecahedron
Print model: robot pendant
Print model: iphone case
unprinted_designs处理后结果 : ['iphone case', 'robot pendant', 'dodecahedron']
The following models have been printed:
dodecahedron
robot pendant
iphone case
2 传递任意数量的参数
方法: *加形参名即可
*toppings实质为: 名为toppings的空元组
然后将所有接搜的值封装到元组中
注意:这样的函数即可接受一个实参也可接受多个实参
def make_pizza(*toppings):
"""打印顾客点的所有配料"""
print(toppings)
make_pizza('pepperoni')
make_pizza('mushroom','green peppers','extra cheese')
结果:
('pepperoni',)
('mushroom', 'green peppers', 'extra cheese')
改进
def make_pizza(*toppings):
"""概述要制作的比萨"""
print("\nMaking a pizza with the following toppings: ")
for topping in toppings:
print("- " + topping)
make_pizza('pepperoni')
make_pizza('mushroom','green peppers','extra cheese')
结果:
Making a pizza with the following toppings:
- pepperoni
Making a pizza with the following toppings:
- mushroom
- green peppers
- extra cheese
2.1 结合使用位置实参和任意数量实参
注意: 函数如果接受不同类型的实参,必须在函数定义中将任意数量实参的形参放在最后
Python先匹配位置实参和关键字实参,最后将剩余实参都放在最后的形参中
def make_pizza(size,*toppings):
"""概述要制作的比萨"""
print("\nMaking a " + str(size) +
"-inch pizza with the following toppings: "
)
for topping in toppings:
print("- " + topping)
make_pizza(16, 'pepperoni')
make_pizza(12, 'mushroom','green peppers','extra cheese')
结果:
Making a 16-inch pizza with the following toppings:
- pepperoni
Making a 12-inch pizza with the following toppings:
- mushroom
- green peppers
- extra cheese
2.2 使用任意数量的关键字实参
方法:将函数写成能接受任意数量的键-值对
写法: ** + 关键字实参变量名(例如: **user_info)
任意创建字实参的实质:名为user_info的空字典
注意:任意数量的关键字实参的形参必须放在最后
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', 'einstein',
location = 'princeton',field = 'physics')
print(user_profile)
结果:
{'first_name': 'albert', 'last_name': 'einstein', 'location': 'princeton', 'field': 'physics'}
总结:位置实参、关键字实参、任意数量实参(任意数量位置实参,任意数量关键字实参)
这三者可以混合使用,但是应该知道使用的时机,以及任意数量实参应该放在新参
列表最后
3 将函存储到模块中
作用:将函数存到模块中可以简化主程序,方便多次调用
模块后缀:.py
导入方式:import语句
pizza.py函数代码
def make_pizza(size,*toppings):
"""概述要制作的比萨"""
print("\nMaking a " + str(size) +
"-inch pizza with the following toppings: "
)
for topping in toppings:
print("- " + topping)
def len_pizza(*toppings):
"""统计食材的数量"""
print("\n食材数量为: ",len(toppings))
从这里开始,下面最终结果是一样的,但是其展示了从函数模块导入函数的几种情况:后面在实际需求中根据需求选择合适的导入方式。
3.1 主函数代码块
import pizza # 导入名为pizza函数模块
pizza.make_pizza(16, 'pepperoni') # .隐式导入函数make_pizza
pizza.make_pizza(12, 'mushroom','green peppers','extra cheese')
结果:
Making a 16-inch pizza with the following toppings:
- pepperoni
Making a 12-inch pizza with the following toppings:
- mushroom
- green peppers
- extra cheese
3.2 导入特定的函数(函数模块中可能有多个功能模块)
from pizza import make_pizza
make_pizza(16, 'pepperoni') # 显示导入函数make_pizza
make_pizza(12, 'mushroom','green peppers','extra cheese')
结果:
Making a 16-inch pizza with the following toppings:
- pepperoni
Making a 12-inch pizza with the following toppings:
- mushroom
- green peppers
- extra cheese
3.3 使用as给函数指定别名
好处:避免与程序中现有的名称冲突,或者简化函数名称(类似于c++的给形参起别名)
from pizza import make_pizza as mp
mp(16, 'pepperoni')
mp(12, 'mushroom','green peppers','extra cheese')
结果:
Making a 16-inch pizza with the following toppings:
- pepperoni
Making a 12-inch pizza with the following toppings:
- mushroom
- green peppers
- extra cheese
3.4 使用as 给模块指定别名
import pizza as p
p.make_pizza(16, 'pepperoni')
p.make_pizza(12, 'mushroom','green peppers','extra cheese')
结果:
Making a 16-inch pizza with the following toppings:
- pepperoni
Making a 12-inch pizza with the following toppings:
- mushroom
- green peppers
- extra cheese
3.5 导入模块中的所有函数
使用星号(*) 运算符可导入模块中所有函数
实质:让python将模块pizza中的每个函数都复制到这个程序文件中
from pizza import *
make_pizza(16, 'pepperoni')
make_pizza(12, 'mushroom','green peppers','extra cheese')
结果:
Making a 16-inch pizza with the following toppings:
- pepperoni
Making a 12-inch pizza with the following toppings:
- mushroom
- green peppers
- extra cheese
4 函数编写指南
1、函数名称应该是描述性名称,即让人知道其功能
2、函数名称组成为小写字母和下划线
3、在函数体内描述函数功能的注解,需采用文档字符串格式
4、给形参指定默认值是,等号两边不要有空格
5、函数调用时,关键字实参的等号两边也不要有空格
6、执行代码块前注意要有缩进(4个空格)
总结:
1、保证原始列表不更改的方式:将列表的副本传入函数(副本:list_name[:], 或者使用深拷贝函数)
2、给函数传任意实参的方式: 1 任意位置实参: 一个星号(*) + 集和名称(例如: * people) ---- 本质创建一个为集和名称的空元组;
2 任意关键字实参: 两个星号(**) + 集和名称(例如 ** people) ----- 本质:创建一个名称为集和名称的空字典
3、位置实参、关键字实参、任意实参可混合使用,但是任意实参的新参名必须位于形参列表的最后
4、函数模块:
函数模块的后缀: .py
函数模块导入方式:# 1、采用import modul_name 导入函数模块
# 2、采用from modul_name import function_name 导入函数名
#3、采用 from module_name import function_name as fn 导入函数名 给函数起别名
# 4、采用 import model_name as mn 导入函数模块 给函数模块起别名
# 5、采用 from model_name import * 导入函数模块中的所有函数