四种:
1.F(arg1,arg2,...)
2.F(arg1,arg2=value2,...)
3.F(*arg1)
4.F(**arg1)
上面这四种方法优先级依次降低,先1,后2,再3,最后4,也就是先把方式1中的arg解析,然后解析方式2中的arg=value,再解析方式3,即是把多出来的arg这种形式的实参组成个tuple传进去,最后把剩下的key=value这种形式的实参组成一个dictionary传给带俩个星号的形参,也就方式4。
>>> def test(x,y=1,*a,**b):
print x,y,a,b
>>> test(1)
1 1 () {}
>>> test(1,2)
1 2 () {}
>>> test(1,2,3)
1 2 (3,) {}
>>> test(1,2,3,4)
1 2 (3, 4) {}
>>> test(x=1,y=2)
1 2 () {}
>>> test(1,a=2)
1 1 () {'a': 2}
>>> test(1,2,3,a=4)
1 2 (3,) {'a': 4}
>>> test(1,2,3,y=4)
Traceback (most recent call last):
File "<pyshell#52>", line 1, in -toplevel-
test(1,2,3,y=4)
TypeError: test() got multiple values for keyword argument 'y'
1.F(arg1,arg2,...)
2.F(arg1,arg2=value2,...)
3.F(*arg1)
4.F(**arg1)
上面这四种方法优先级依次降低,先1,后2,再3,最后4,也就是先把方式1中的arg解析,然后解析方式2中的arg=value,再解析方式3,即是把多出来的arg这种形式的实参组成个tuple传进去,最后把剩下的key=value这种形式的实参组成一个dictionary传给带俩个星号的形参,也就方式4。
>>> def test(x,y=1,*a,**b):
print x,y,a,b
>>> test(1)
1 1 () {}
>>> test(1,2)
1 2 () {}
>>> test(1,2,3)
1 2 (3,) {}
>>> test(1,2,3,4)
1 2 (3, 4) {}
>>> test(x=1,y=2)
1 2 () {}
>>> test(1,a=2)
1 1 () {'a': 2}
>>> test(1,2,3,a=4)
1 2 (3,) {'a': 4}
>>> test(1,2,3,y=4)
Traceback (most recent call last):
File "<pyshell#52>", line 1, in -toplevel-
test(1,2,3,y=4)
TypeError: test() got multiple values for keyword argument 'y'