1. 常规参数(顺序传入参数)
>>> def f(a,b,c):
print(a,b,c)
>>> f(1,2,3)
1 2 3
参数进行函数的顺序是从左到右.2.关键字参数
>>> def f(a,b,c):
print(a,b,c)
>>> f(c=3,b=2,a=1)
1 2 3
函数通过变量名识别参数
3.默认参数
>>> def f(a,b=2,c=3):
print(a,b,c)
>>> f(1)
1 2 3
>>> f(a=1)
1 2 3
>>> f(1,4)
1 4 3
>>> f(1,4,5)
1 4 5
>>> f(1,c=6)
1 2 6
4.1.使用元组收集不匹配的位置参数
>>> def f(*args):
print(args)
>>> f()
()
>>> f(1)
(1,)
>>> f(1,2,3,4)
(1, 2, 3, 4)
4.2. 使用字典收集不匹配的位置参数(只对关键字参数有效)
>>> def f(**args):
print(args)
>>> f()
{}
>>> f(a=1,b=2)
{'a': 1, 'b': 2}
>>> f(1,2)
Traceback (most recent call last):
File "<pyshell#36>", line 1, in <module>
f(1,2)
TypeError: f() takes 0 positional arguments but 2 were given
>>> def f(a,*pargs,**kargs):
print(a,pargs,kargs)
>>> f(1,2,3,x=1,y=2)
1 (2, 3) {'y': 2, 'x': 1}
>>> def func(a,b,c,d):
print(a,b,c,d)
>>> func(*(1,2),**{'d':4,'c':4})
1 2 4 4
>>> func(1,*(2,3),**{'d':4})
1 2 3 4
>>> func(1,*(2,),c=3,**{'d':4})
1 2 3 4