Python中函数的参数有4种形式,分别是:
第一种:位置或关键字参数
这种参数是Python中默认的参数类型,定义这种参数后,可以通过位置参数,或者关键字参数的形式传递参数:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
##
位置或者关键字参数##
这个是Python的默认参数类型##
示例:arg2提供了默认valuedef
func(arg1, arg2="World!"): print
arg1, arg2##
func可以通过位置参数形式调用func("Hello", "MitchellChu")##
也可以通过关键字参数的形式来调用funcfunc(arg1="Hello",
arg2="World!")##
当然,混合的方式也是完全没有问题的func("Hello",
arg2="World!")##
不过如果你不能将关键字参数优先于位置参数传递给函数(方法)##
这个调用方法是不能接受的,因为优先级不一样.后面会说func(arg1="Hello", "World!") ##
ERROR |
第二种方式:仅适用位置参数的形式
这种形式在需要将参数传递给函数(方法)时,仅能通过位置参数的传递方式。这种形式对于Python的开发者来说,暂时并没有办法使用。这种形式现在仅存在Python的很多内建的函数上:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
##
Positional-only parameter has no syntax to define##
虽然无定义方法,但内建的很多函数都是仅接受位置参数的abs(-3) ##
correctabs(a=3) ##
wrong##
Traceback (most recent call last):##
File "<stdin>", line 1, in <module>##
TypeError: abs() takes no keyword argumentspow(x=2,y=3)##
Traceback (most recent call last):##
File "<stdin>", line 1, in <module>##
TypeError: pow() takes no keyword argumentspow(2,3)##
8 |
第三种:任意数量的位置参数(带单个星号参数)
任意数量的位置参数在定义的时候是需要一个星号前缀来表示,在传递参数的时候,可以在原有参数的后面添加任意多个参数,这些参数将会被放在元组内提供给函数(方法):
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
##
var-positional parameter##
定义的时候,我们需要添加单个星号作为前缀def
func(arg1, arg2, *args): print
arg1, arg2, args##
调用的时候,前面两个必须在前面##
前两个参数是位置或关键字参数的形式##
所以你可以使用这种参数的任一合法的传递方法func("hello", "Tuple,
values is:", 2, 3, 3, 4)##
Output:##
hello Tuple, values is: (2, 3, 3, 4)##
多余的参数将自动被放入元组中提供给函数使用##
如果你需要传递元组给函数##
你需要在传递的过程中添加*号##
请看下面例子中的输出差异:func("hello", "Tuple,
values is:",
(2, 3, 3, 4))##
Output:##
hello Tuple, values is: ((2, 3, 3, 4),)func("hello", "Tuple,
values is:", *(2, 3, 3, 4))##
Output:##
hello Tuple, values is: (2, 3, 3, 4) |
第四种:任意数量的关键字参数(带两个星号参数)
任意数量的关键字参数在定义的时候,参数名称前面需要有两个星号(**)作为前缀,这样定义出来的参数,在传递参数的时候,可以在原有的参数后面添加任意多个关键字参数,关键字参数是使用[参数名称=参数值]的形式进行传递:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
##
var-keywords parameter##
定义的时候,需要两个星号作为前缀def
func(arg1, arg2, **kwargs): print
arg1, arg2, kwargsfunc("hello", "Dict,
values is:",
x=2,
y=3,
z=3)##
Output:##
多余的参数将自动被放入字典中提供给函数使用##
如果你需要直接传递字典给函数##
你需要在传递的过程中添加**##
此时如果还有关键字参数应在字典前提供完成##
不能在字典后再提供##
请看下面例子中的输出差异:func("hello", "Dict.,
values is:", **{‘x‘:2, ‘y‘:3, ‘z‘:3})##
hello Dict., values is: {‘y‘: 3, ‘x‘: 2, ‘z‘: 3}func("hello", "Dict.,
values is:", **{‘x‘:2, ‘y‘:3, ‘z‘:3,})##
hello Dict., values is: {‘y‘: 3, ‘x‘: 2, ‘z‘: 3}func("hello", "Dict.,
values is:",
{‘x‘:2, ‘y‘:3, ‘z‘:3})##
Traceback (most recent call last):##
File "<stdin>", line 1, in <module>##
TypeError: func() takes exactly 2 arguments (3 given)func("hello", "Dict.,
values is:",
s=3, **{‘x‘:2, ‘y‘:3, ‘z‘:3,})##
hello Dict., values is: {‘y‘: 3, ‘x‘: 2, ‘s‘: 3, ‘z‘: 3}##
提供了重复的参数func("hello", "Dict.,
values is:",
y=3, **{‘x‘:2, ‘y‘:3, ‘z‘:3,})##
Traceback (most recent call last):##
File "<stdin>", line 1, in <module>##
TypeError: func() got multiple values for keyword argument ‘y‘ |
总结:四种参数形式中仅有第二种Python没有提供定义的方法,其他三种在定义的时候也需要注意,定义的时候应该根据Python的解析规律进行定义,其中:
注意:任意数量位置参数和任意数量关键字参数只能在定义中定义一次。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
##
各种参数的混合使用例子##
Author: MitchellChudef
func(arg1, arg2=‘default‘, *args, **kwargs): print
"arg1=%s, arg2=%s, args=%s, kwargs=%s"
%
(arg1, arg2, args, kwargs)func(1) ##
correctfunc(1,2) ##
correctfunc(1,2,3,4) ##
correctfunc(1,2,3,4,x=1,y=2) ##
correctfunc(1,2,x=1) ##
correctfunc(x=1) ##
wrongfunc(arg1=1) ##
correctfunc(1,x=1) ##
correct##
可以将例子保存到parameter.py文件##
而后运行python /path/to/parameter.py |
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/nyist327/article/details/48057359