参考 https://docs.python.org/zh-cn/3.9/tutorial/controlflow.html#more-on-defining-functions
参数默认值
参考 https://docs.python.org/zh-cn/3.9/tutorial/controlflow.html#default-argument-values
如何使用
最有用的形式是对一个或多个参数指定一个默认值。这样创建的函数,可以用比定义时允许的更少的参数调用,比如:
def ask_ok(prompt, retries=4, reminder='Please try again!'):while True:ok = input(prompt)if ok in ('y', 'ye', 'yes'):return Trueif ok in ('n', 'no', 'nop', 'nope'):return Falseretries = retries - 1if retries < 0:raise ValueError('invalid user response')print(reminder)
这个函数可以通过几种方式调用:
- 只给出必需的参数:
ask_ok('Do you really want to quit?') - 给出一个可选的参数:
ask_ok('OK to overwrite the file?', 2) - 或者给出所有的参数:
ask_ok('OK to overwrite the file?', 2, 'Come on, only yes or no!')默认值的赋值时机
默认值是在 定义过程 中在函数定义处计算的,所以 ```python i = 5
def f(arg=i): print(arg) # 5
i = 6 f()
```pythoni = 5def f(arg=i):print(arg) # 6i = 6f(i)
这个在上一节《函数的定义和使用》的函数执行流程做过说明,不难理解
重要警告
默认值只会执行一次。这条规则在默认值为可变对象(列表、字典以及大多数类实例)时很重要。比如,下面的函数会存储在后续调用中传递给它的参数:
def f(a, L=[]):L.append(a)return Lprint(f(1)) # [1]print(f(2)) # [1, 2]print(f(3)) # [1, 2, 3]
如何理解
这里强调的是理解,方便记忆,并未确定就是这种原理
默认参数可理解为全局变量,但是全局作用域不能访问,函数作用域可以访问
L1= []def f(a, L=L1):L.append(a)return Lprint(f(1)) # [1]print(f(2)) # [1, 2]print(f(3)) # [1, 2, 3]print(L) # NameError: name 'L' is not defined
【关键字参数】
作用:方便函数调用时灵活指定参数,正常情况下,给函数传参数要按顺序,不想按顺序就可以用关键参数。
注意:在函数调用中,关键字参数必须跟随在位置参数的后面。
def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):print("-- This parrot wouldn't", action, end=' ')print("if you put", voltage, "volts through it.")print("-- Lovely plumage, the", type)print("-- It's", state, "!")
接受一个必需的参数(voltage)和三个可选的参数(state, action,和 type)。这个函数可以通过下面的任何一种方式调用:
parrot(1000) # 1 positional argumentparrot(voltage=1000) # 1 keyword argumentparrot(voltage=1000000, action='VOOOOOM') # 2 keyword argumentsparrot(action='VOOOOOM', voltage=1000000) # 2 keyword argumentsparrot('a million', 'bereft of life', 'jump') # 3 positional argumentsparrot('a thousand', state='pushing up the daisies') # 1 positional, 1 keyword
但下面的函数调用都是无效的:
parrot() # required argument missingparrot(voltage=5.0, 'dead') # non-keyword argument after a keyword argumentparrot(110, voltage=220) # duplicate value for the same argumentparrot(actor='John Cleese') # unknown keyword argument
【特殊参数】
这个知识点作为了解即可,属于新特性,用于对参数做一些限制
参考 https://docs.python.org/zh-cn/3.9/tutorial/controlflow.html#special-parameters
解包参数列表
当参数已经在列表或元组中但要为需要单独位置参数的函数调用解包时,会发生相反的情况。例如,内置的
[range()](https://docs.python.org/zh-cn/3.9/library/stdtypes.html#range)函数需要单独的 start 和 stop 参数。如果它们不能单独使用,可以使用*操作符 来编写函数调用以便从列表或元组中解包参数:
解包列表或元组
>>> list(range(3, 6)) # 使用单独参数的正常调用[3, 4, 5]>>> args = [3, 6]>>> list(range(*args)) # 调用时参数从列表中解包[3, 4, 5]>>> args = (3, 6)>>> list(range(*args)) # 调用时参数从列表中解包[3, 4, 5]
解包字典
>>> def parrot(voltage, state='a stiff', action='voom'):... print("-- This parrot wouldn't", action, end=' ')... print("if you put", voltage, "volts through it.", end=' ')... print("E's", state, "!")...>>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}>>> parrot(**d)-- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !
任意的参数列表
最后,最不常用的选项是可以使用任意数量的参数调用函数。这些参数会被包含在一个元组里(参见 元组和序列)。在可变数量的参数之前,可能会出现零个或多个普通参数。
注解:若函数在定义时不确定想传入多少个参数,就可以使用任意的参数列表
*args
def write_multiple_items(file, *args):print(args)write_multiple_items(1,2,3) # 输出: (2, 3)write_multiple_items(1,2,3,4) # 输出: (2, 3, 4)
def write_multiple_items(file, name='ecithy', *args):print(args, name)write_multiple_items(1,2,3) # 输出: (3,) 2write_multiple_items(1,2,3,4) # 输出: (3, 4) 2
# 不推荐这样使用def write_multiple_items(file, *args, name='ecithy'):print(args, name)write_multiple_items(1,2,3) # 输出: (2, 3) ecithywrite_multiple_items(1,2,3,4) # 输出: (2, 3, 4) ecithy
**kwargs
def write_multiple_items(file, **kargs):print(file, kargs)write_multiple_items('1.txt', age=18, phone='110')# 输出: 1.txt {'age': 18, 'phone': '110'}
args + *kwargs
def write_multiple_items(file, *args, **kwargs):print(file, args, kwargs)write_multiple_items('1.txt', 1, 2 , age=18, phone='110')# 输出: 1.txt (1, 2) {'age': 18, 'phone': '110'}
总结
一般用法:普通参数(位置参数、默认参数放在前,任意的参数列表放在最后
注意:
*args、**kargs中的args、kargs不是固定写法,但是一般这样写*args会把多传入的参数变成一个元组形式,**kargs会把多传入的参数变成一个字典形式解包参数列表+任意的参数列表
推荐这种写法,原因如下:
语法间接
- 便于记忆:形参和实参参数符号一致(均为、*)
*args
```python def write_multiple_items(file, *args): print(file, args)
t = (18, ‘110’)
write_multiple_items(‘1.txt’, *t)
输出: 1.txt (18, ‘110’)
<a name="UJgEZ"></a>### **kargs```pythondef write_multiple_items(file, **kargs):print(file, kargs)d = dict(age=18, phone='110')write_multiple_items('1.txt', **d)# 输出: 1.txt {'age': 18, 'phone': '110'}
args + *kwargs
def write_multiple_items(file, *args, **kwargs):print(file, args, kwargs)t = (18, '110')d = dict(age=18, phone='110')write_multiple_items('1.txt', *t, **d)# 输出: 1.txt (18, '110') {'age': 18, 'phone': '110'}
