Python支持可变参数,通过*args和**kwargs来指定,示例如下:
def test_kwargs(first, *args, **kwargs):
print 'Required argument: ', first
for v in args:
print 'Optional argument (*args): ', v
for k, v in kwargs.items():
print 'Optional argument %s (*kwargs): %s' % (k, v)
test_kwargs(1, 2, 3, 4, k1=5, k2=6)
# results:
# Required argument: 1
# Optional argument (*args): 2
# Optional argument (*args): 3
# Optional argument (*args): 4
# Optional argument k2 (*kwargs): 6
# Optional argument k1 (*kwargs): 5
其中*args是可变列表参数,**kwargs是可变字典参数。