Python函数的各类参数(含星号参数)

Python中函数的参数有4种形式,分别是:asp.net

  1. 位置或关键字参数(Positional-or-keyword parameter)ide

  2. 仅位置的参数(Positional-only parameter)函数

  3. 任意数量的位置参数(var-positional parameter)spa

  4. 任意数量的关键字参数(var-keyword parameter).net

第一种:位置或关键字参数code

这种参数是Python中默认的参数类型,定义这种参数后,能够经过位置参数,或者关键字参数的形式传递参数:blog

code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
## 位置或者关键字参数
## 这个是Python的默认参数类型
## 示例:arg2提供了默认value
def func(arg1, arg2 = "World!" ):
     print arg1, arg2
 
## func能够经过位置参数形式调用
func( "Hello" , "MitchellChu" )
 
## 也能够经过关键字参数的形式来调用func
func(arg1 = "Hello" , arg2 = "World!" )
 
## 固然,混合的方式也是彻底没有问题的
func( "Hello" , arg2 = "World!" )
 
## 不过若是你不能将关键字参数优先于位置参数传递给函数(方法)
## 这个调用方法是不能接受的,由于优先级不同.后面会说
func(arg1 = "Hello" , "World!" ) ## ERROR

 第二种方式:仅适用位置参数的形式ip

这种形式在须要将参数传递给函数(方法)时,仅能经过位置参数的传递方式。这种形式对于Python的开发者来讲,暂时并无办法使用。这种形式如今仅存在Python的不少内建的函数上:ci

code
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 ) ## correct
abs (a = 3 ) ## wrong
 
## Traceback (most recent call last):
##   File "<stdin>", line 1, in <module>
## TypeError: abs() takes no keyword arguments
 
 
pow (x = 2 ,y = 3 )
## Traceback (most recent call last):
##   File "<stdin>", line 1, in <module>
## TypeError: pow() takes no keyword arguments
 
pow ( 2 , 3 )
## 8

 第三种:任意数量的位置参数(带单个星号参数开发

任意数量的位置参数在定义的时候是须要一个星号前缀来表示,在传递参数的时候,能够在原有参数的后面添加任意多个参数,这些参数将会被放在元组内提供给函数(方法):

code
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)

第四种:任意数量的关键字参数(带两个星号参数)

任意数量的关键字参数在定义的时候,参数名称前面须要有两个星号(**)做为前缀,这样定义出来的参数,在传递参数的时候,能够在原有的参数后面添加任意多个关键字参数,关键字参数是使用[参数名称=参数值]的形式进行传递:

code
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, kwargs
 
func( "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. 任意数量关键字参数应该放在任意数量位置参数的后面

注意:任意数量位置参数和任意数量关键字参数只能在定义中定义一次。

code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
## 各类参数的混合使用例子
## Author: MitchellChu
 
def func(arg1, arg2 = 'default' , * args, * * kwargs):
     print "arg1=%s, arg2=%s, args=%s, kwargs=%s" % (arg1, arg2, args, kwargs)
 
 
func( 1 ) ## correct
func( 1 , 2 ) ## correct
func( 1 , 2 , 3 , 4 ) ## correct
func( 1 , 2 , 3 , 4 ,x = 1 ,y = 2 ) ## correct
func( 1 , 2 ,x = 1 ) ## correct
 
func(x = 1 ) ## wrong
func(arg1 = 1 ) ## correct
func( 1 ,x = 1 ) ## correct
func(arg1, arg2=, *args, **kwargs):
    (% (arg1, arg2, args, kwargs))
func(,,,=,=,=,=)

arg1=1, arg2=2, args=(3,), kwargs={'a': 4, 'x': 1, 'z': 3, 'y': 2}

相关文章
相关标签/搜索