by 《Python编程从入门到实践》
四、关于函数
(21)函数的参数
形参是函数完成其工作所需的一项信息;而实参是调用函数时传递给函数的信息。
向函数传递实参的方式很多,可使用位置实参,这要求实参的顺序与形参的顺序相同;也可以使用关键字实参,其中每个实参都由变量名和值组成;还可以使用列表和字典
#等效的函数调用
#使用默认值时,在形参列表中必须先列出没有默认值的形参,
#再列出有默认值的形参。这让Pyhon依然能够正确地解读位置实参。
def describe_pet(pet_name, animal_type='dog'):
"""显示宠物的信息"""
print("I have a " + animal_type + ".")
print("Its name is " + pet_name.title() + ".")
#下面对这个函数的所有调用都可行
#一条名为Allen的小狗
describe_pet('allen')
describe_pet(pet_name='allen')
#一只名为Bob的猫
describe_pet('bob','cat')
describe_pet(pet_name='bob',animal_type='cat')
describe_pet(animal_type='cat',pet_name='bob')
(22)函数返回值——简单值
#返回姓名
#Python将非空字符串解读为Tue
def get_name(first_name,last_name,middle_name=''):
"""返回整洁的姓名"""
if middle_name:
full_name = first_name + ' ' + middle_name + ' ' + last_name
else:
full_name = first_name + ' ' + last_name
return full_name.title()
per = get_name('jimi','hendrix')
print(per)
qer = get_name('john','hooker','lee')
print(qer)
运行结果
Jimi Hendrix
John Lee Hooker
(23)函数返回值——字典
#返回字典
def build_person(first_name, last_name, age=''):
person = {'first':first_name, 'last':last_name}
if age:
person['age'] = age
return person
#也可以在末尾指定age=27
per = build_person('allen','worker',27)
print(per)
per = build_person('allen',age=27,last_name='worker')
print(per)
这里是引用
{‘first’: ‘allen’, ‘last’: ‘worker’, ‘age’: 27}
{‘first’: ‘allen’, ‘last’: ‘worker’, ‘age’: 27}
虽然第二种传参方式也可以,但是不建议!
位置参数就是位置参数,关键字就是关键字,混用会引起混乱!
(24)结合while循环使用函数
def get_name(first_name,last_name):
"""返回整洁的姓名"""
full_name = first_name + ' ' + last_name
return full_name.title()
while True:
print("Tell me your name")
print("(enter 'q' at any time to quit)")
f_name = input("First Name: ")
if f_name == 'q':
break
l_name = input("Last Name: ")
if l_name == 'q':
break
name = get_name(f_name,l_name)
print(name)
我们添加了一条消息来告诉用户如何退出,然后在每次提示用户输入时,都检查他输入的是否是退出值,如果是,就退出循环。现在,这个程序将不断地问候,直到用户输入的姓或名为‘q’为止。
(25)传递列表
简单示例如下:
def greet_users(names):
“””向列表中的每位用户都发出简单问候”””
for name in names:
msg = ‘hello’ + name.title()
print(msg)
usernames = [‘allen’, ‘bob’, ‘calvin’]
greet_users(usernames)
在函数中修改列表
将列表传递给函数后,函数就可对其进行修改。在函数中对这个列表所做的任何修改都是永久性的。
def print_models(designs,models):
"""
打印每个设计,直到没有未打印的设计为止
打印每个设计后,都将其移动到列表models中
"""
while designs:
current_design = designs.pop()
print("print: " + current_design)
models.append(current_design)
def show_models(models):
"""
显示所有打印好的模型
"""
print("Printed Models: ")
for m in models:
print(m)
#创建一个列表,其中包含一些要打印的设计
unprinted_designs = ['iphone','huawei','sumsang']
completed_models = []
print_models(unprinted_designs, completed_models)
show_models(completed_models)
print("Let's see what in unprinted_designs: " + str(unprinted_designs))
print("Let's see what in completed_models: " + str(completed_models))
运行结果
print: sumsang
print: huawei
print: iphone
Printed Models:
sumsang
huawei
iphone
Let’s see what in unprinted_designs: []
Let’s see what in completed_models: [‘sumsang’, ‘huawei’, ‘iphone’]
禁止函数修改列表
假设在前例中,即便打印所有设计后,也要保留原来的未打印的设计列表,以供备案。
#将列表的副本传递给函数
#function_name(list_name[:])
print_models(unprinted_designs[:], completed_models)
运行结果
print: sumsang
print: huawei
print: iphone
Printed Models:
sumsang
huawei
iphone
Let’s see what in unprinted_designs: [‘iphone’, ‘huawei’, ‘sumsang’]
Let’s see what in completed_models: [‘sumsang’, ‘huawei’, ‘iphone’]
(26)传递任意数量的实参
形参名前面加星号*让Python创建一个以形参名命名的空元祖,并将收到的所有值都封装到这个元组之中。
def make_pizza(*toppings):
"""
概述要制作的披萨
"""
print("Making a pizza with the following toppings:")
for topping in toppings:
print("-" + topping)
#开始制作披萨
make_pizza('pepperoni')
make_pizza('mushrooms','green peppers','extra cheese')
运行结果
Making a pizza with the following toppings:
-pepperoni
Making a pizza with the following toppings:
-mushrooms
-green peppers
-extra cheese
如果要让函数接受不同类型的实参,必须在函数定义中将接纳任意数量实参的形参放在最后。Python先匹配位置实参和关键字实参,再将余下的实参都收集到最后一个形参中。例如在上例中,若函数还需要一个表示披萨尺寸的实参,必须将该形参放在形参*toppings的前面。
def make_pizza(size, *toppings):
"""
概述要制作的披萨
"""
print("Making a " + str(size) + "-inch pizza with the following toppings:")
for topping in toppings:
print("-" + topping)
#开始制作披萨
make_pizza(12, 'pepperoni')
make_pizza(16, 'mushrooms','green peppers','extra cheese')
(27)使用任意数量的关键字实参
有时候,需要接受任意数量的实参,但预先不知道传递给函数的会是什么样的信息。在这种情况下,可将函数编写成能够接受任意数量的键-值对——调用语句提供了多少就接受多少。一个这样的示例是创建用户简介:你知道你将收到有关用户的信息,但不确定会是什么样的信息。在下面的示例中,函数接收名和姓,同时还接收任意数量的关键字实参:
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’}
函数build_profile()的定义要求提供名和姓,同时允许用户根据需要提供任意数量的名称-值对。
形参**user_info中的两个星号让Python创建一个名为user_info的空字典,并将收到的所有名称-值对都封装到这个字典中。在这个函数中,可以像访问其它字典那样访问user_info中的名称-值对。
(28)将函数存储在模块中
模块是扩展名为 .py 的文件,包含要导入到程序中的代码。
例如:创建一个包含函数make_pizza()的模块pizza.py
# pizza.py
def make_pizza(size, *toppings):
"""
概述要制作的披萨
"""
print("Msking a " + str(size) +"-inch pizza with the following toppings:")
for topping in toppings:
print("-" + topping)
在pizza.py所在的目录中创建另一个名为make_pizza.py的文件,在这个文件中导入刚创建的模块,再调用函数make_pizza()两次:
# 导入模块后,module_name.function_name()
import pizza
pizza.make_pizza(12, 'pepperoni')
pizza.make_pizza(16,'mushrooms','green peppers','extra cheese')
运行结果
Making a 12-inch pizza with the following toppings:
-pepperoni
Making a 16-inch pizza with the following toppings:
-mushrooms
-green peppers
-extra cheese
导入模块中的特定函数:
from module_name import function_name
from module_name import function_0, function_1, function_2
from pizza import make_pizza
make_pizza(12, 'pepperoni')
make_pizza(16,'mushrooms','green peppers','extra cheese')
使用as给函数指定别名
from module_name import function_name as fn
from pizza import make_pizza as mp
mp(12, 'pepperoni')
mp(16,'mushrooms','green peppers','extra cheese')
使用as给模块指定别名
import module_name as mn
import pizza as p
p.make_pizza(12, 'pepperoni')
p.make_pizza(16,'mushrooms','green peppers','extra cheese')
导入模块中的所有函数
使用星号(*)运算符可让Python导入模块中的所有函数
from pizza import *
pizza.make_pizza(12, 'pepperoni')
pizza.make_pizza(16,'mushrooms','green peppers','extra cheese')