函数参数总结
1.位置匹配 func(name)
2.关键字匹配 func(key=value)
3.收集匹配
1.元组收集 func(name,arg1,arg2)
2.字典收集 func(name,key1=value1,key1=value2)
4.参数顺序
1.位置匹配 func(name)
def func(arg1,arg2,arg3): return arg1,arg2,arg3 print func(1,2,3)
(1, 2, 3)
2.关键字匹配 func(key=value)
和顺序无关,可以有默认值。
def func1(k1=‘‘,k2=None,k3=‘‘): return k1,k2,k3 print (func1(k3=5,k1=4,k2=3)) print (func1(k1=1,k3=2))
(4, 3, 5)
(1, None, 2)
3.收集匹配
1.元组收集 func(name,arg1,arg2)
2.字典收集 func(name,key1=value1,key1=value2)
def func2(a,d,b=4,*kargs,**kwargs): return kargs print (func2(2,3,4,5,6,7,9,[1,2,3,4],{1:2,3:4})) def func2(*kargs,**kwargs): return kargs print (func2(2,3,4,5,6,7,9,[1,2,3,4],{1:2,3:4})) def func2(*kargs,**kwargs): return kargs print (func2(2,3,4,{1:2,3:4},[1,2,3,4],5,6,7,9))
(5, 6, 7, 9, [1, 2, 3, 4], {1: 2, 3: 4})
(2, 3, 4, 5, 6, 7, 9, [1, 2, 3, 4], {1: 2, 3: 4})
(2, 3, 4, {1: 2, 3: 4}, [1, 2, 3, 4], 5, 6, 7, 9)
4.参数顺序
1.先是位置匹配的参数 a, d
2.再是关键字匹配的参数 b=4
3.收集匹配的元组参数 *kargs 一个星号是元祖
4.收集匹配的关键字参数 **kwargs 两个星号是字典
原文:http://www.cnblogs.com/think-and-do/p/6357417.html