这两个都是可变参数,可以传入任意个数的参数
*args是非关键字参数,用于tuple,**kw是关键字参数,用于dict
同时使用*args和**kwargs时,必须*args参数列要在**kwargs前
代码
def foo(*args, **kw): print ('args = ', args) print ('kw = ', kw) print ('---------------------------------------') return foo(1,2,3,4) foo(a=1,b=2,c=3) foo(1,2,3,4, a=1,b=2,c=3) foo('a', 1, None, a=1, b='2', c=3)
运行结果
args = (1, 2, 3, 4) kw = {} --------------------------------------- args = () kw = {'a': 1, 'c': 3, 'b': 2} --------------------------------------- args = (1, 2, 3, 4) kw = {'a': 1, 'c': 3, 'b': 2} --------------------------------------- args = ('a', 1, None) kw = {'a': 1, 'c': 3, 'b': '2'} ---------------------------------------