*args
和**kargs
是一种约定俗称的用法,目的是用于传入不定数量的参数,前者把传入的参数变成一个tuple,后者把传入的参数编程一个字典
1 | In [1]: def foo(*args): |
The **kwargs
will give you all keyword arguments except for those corresponding to a formal parameter as a dictionary.
1 | In [5]: def bar(**kwargs): |
Both idioms can be mixed with normal arguments to allow a set of fixed and some variable arguments:
1 | def foo(kind, *args, **kwargs): |
Another usage of the *l
idiom is to unpack argument lists when calling a function.
*
的另外一个用途就是unpack(打开包裹),比如调用zip的时候,zip的用法是传入n个对象进行zip,但是每一个都是单独的,比如你可以zip(*[1,2,3])
1 | In [9]: def foo(bar, lee): |