变长参数
>>> def f(name, *tupleArg, **dictArg):
... print type(tupleArg), tupleArg
... print type(dictArg), dictArg
...
>>> f('a')
<type 'tuple'> ()
<type 'dict'> {}
>>> f('a', 'haha', '1', arg1=1, arg2=2)
<type 'tuple'> ('haha', '1')
<type 'dict'> {
'arg1': 1, 'arg2': 2}
>>> f('a', 'haha', '1', 'arg3'=1)
File "<stdin>", line 1
SyntaxError: keyword can't be an expression
>>> f('a', 'haha', '1', {'arg3'=1})
File "<stdin>", line 1
f('a', 'haha', '1', {'arg3'=1})
^
SyntaxError: invalid syntax
>>> f('a', 'haha', '1', {'arg3':1})
<type 'tuple'> ('haha', '1', {'arg3': 1})
<type 'dict'> {}
>>>
**与字典,*与元组的相互转换
>>> f('a', 'haha', '1', {
'arg3':1})
<type 'tuple'> ('haha', '1', {
'arg3': 1})
<type 'dict'> {}
>>> f('a', 'haha', '1', **{
'arg3':1})
<type 'tuple'> ('haha', '1')
<type 'dict'> {
'arg3': 1}
>>> f('a', 'haha', '1', *(2, 3, 4))
<type 'tuple'> ('haha', '1', 2, 3, 4)
<type 'dict'> {}
函数调用
>>> def f(name, *tupleArg, **dictArg):
... print type(tupleArg), tupleArg
... print type(dictArg), dictArg
...
>>> def g(name, *tupleArg, **dictArg):
... print type(tupleArg), tupleArg
... print type(dictArg), dictArg
... f(name, *tupleArg, **dictArg)
...
>>> g('a', 1, 2, 3, a=1, b=2, c=3)
<type 'tuple'> (1, 2, 3)
<type 'dict'> {
'a': 1, 'c': 3, 'b': 2}
<type 'tuple'> (