Python函数提供多个参数类型供用户选择
- 位置参数:以参数声明的位置按顺序赋值
- 默认参数:声明函数时已经进行赋值,传递参数时可以再传新的参数值,也可以不传。必须要在位置参数后面
- 可变参数:以tuple的类型进行赋值的参数,以*进行声明
- 关键字参数:以组装成dict的形式进行赋值的参数值,每个参数值对应一个关键字,以**进行声明
- 命名关键字参数:
>>> def teset(a,b,*,c):
... print('c是命令关键字参数,赋值时必须加上参数名c')
...
>>> test(1,2,9)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: test() takes 2 positional arguments but 3 were given
>>> test(1,2,c=9)
a: 1 b: 2 c: {'c': 9}
>>>
对参数类型进行安排的时候大多选择:位置参数、默认参数、可变参数、命名关键字参数、关键字参数
但是:
一、默认参数必须要在位置参数后面,但默认参数可以不用放在最后,不过如果要更新默认的值,要特别指定,否则参数转变成元组,赋给可变参数。如果在前,则是在赋予位置参数与默认参数后,再给可变参数赋值,而不用特殊指定给默认参数,此即按声明顺序赋值,如果再指定赋值,则会报告多个参数值的错误。
>>> def test(a, *b, c=2):
... print("把可变参数放在默认参数前面")
...
>>> test(1)
把可变参数放在默认参数前面
>>>
>>> def test2(a, *b, c=2):
... print('a:',a, '\nb:',b, '\nc:',c)
...
>>> test2(1,2,3,4,5)
a: 1
b: (2, 3, 4, 5)
c: 2
>>> test2(1,2,3,c=4,5)
File "<stdin>", line 1
SyntaxError: positional argument follows keyword argument
>>> test2(1,2,3,4,c=5)
a: 1
b: (2, 3, 4)
c: 5
>>> def test3(a, b=9, *c):
... #a是位置参数,b是默认参数,c是可变参数
... print('a:',a, '\nb:',b, '\nc:',c)
...
>>> test3(1,2,3,4,5)
a: 1
b: 2
c: (3, 4, 5)
>>> test3(1,2,3,4,5,b=8)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: test3() got multiple values for argument 'b'
>>> def test3(a, b=9, *c):
... #a是位置参数,b是默认参数,c是可变参数
... print('a:',a, '\nb:',b, '\nc:',c)
...
>>> test3(1,2,3,4,5)
a: 1
b: 2
c: (3, 4, 5)
>>> test3(1,2,3,4,5,b=8)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: test3() got multiple values for argument 'b'
>>> test3(1,b=8,3,4,5)
File "<stdin>", line 1
SyntaxError: positional argument follows keyword argument
关键字参数:1.可以直接在最后指定一个参数名与参数值进行赋值,**会把传入的值变成字典形式。
2.也可以把整个字典传入,不过要加**号
3.字典内的值也行,只要把字典内的值取出再像1步那样赋值,即-->test(3,4,language=d['language'])
>>> #关键字参数
...
>>> def test(a,b,**c):
... print('a:',a, '\nb:',b, '\nc:',c)
...
>>> test(3,4,language='Python')
a: 3
b: 4
c: {'language': 'Python'}
>>> d = {'language':'python'}
>>>
>>> test(5,6,d)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: test() takes 2 positional arguments but 3 were given
>>> test(5,6,**d)
a: 5
b: 6
c: {'language': 'python'}
命令关键字参数:以*为分隔符,后面的参数名即关键字,且传入时必须写上,内部也
>>> #命名关键字参数
...
>>> def test(a,b,*,c,d):
... print('a:',a, '\nb:',b, '\nc:',c, '\nd:',d)
...
>>> test(2,3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: test() missing 2 required keyword-only arguments: 'c' and 'd'
>>> test(2,3,7,8)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: test() takes 2 positional arguments but 4 were given
>>> test(2,3,c=7,d=8)
a: 2
b: 3
c: 7
d: 8
>>>
>>>
>>> #*号后面的参数即命令关键字参数,且传入的时候必须写上对象名,否则如上报错
...
>>> def test2(a,b,*,c='Python',d)
File "<stdin>", line 1
def test2(a,b,*,c='Python',d)
^
SyntaxError: invalid syntax
>>> def test2(a,b,*,c='Python',d):#c为命名参数类型中的缺省参数
... print('a:',a, '\nb:',b, '\nc:',c, '\nd:',d)
...
>>> test2(8,9,d='数学')
a: 8
b: 9
c: Python
d: 数学
>>> test2(3,2,c='C language',d='English')
a: 3
b: 2
c: C language
d: English
>>>
不过,命令关键字不以字典形式存储,其大多用于,关键字参数的名称
>>> def test3(a,b,*,c='Python',d):
... print(c)
... print(d)
...
>>> test3(1,2,c='math',d='kfc')
math
kfc