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