关于Python中 *args 和 **kwargs的用法,网上有很多介绍文章,还是先看一段官方的解释:
When a final formal parameter of the form **name is present, it receives a dictionary (see Mapping Types — dict) containing all keyword arguments except for those corresponding to a formal parameter. This may be combined with a formal parameter of the form *name (described in the next subsection) which receives a tuple containing the positional arguments beyond the formal parameter list. (*name must occur before **name.)
首先需要了解Python函数传递参数的方式有两种:
位置参数(positional argument)
关键词参数(keyword argument)
然后再来看*args(*name)与**kwargs(**name)的区别,两者都是python中的可变参数:
*args表示任何多个无名参数,它本质是一个tuple;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | >>> def args_one(narg, *args): ... print("Normal arg:", narg) ... for arg in args: ... print("other args:", arg) ... >>> args_one(1, 2, 3, 4, 5) Normal arg: 1 other args: 2 other args: 3 other args: 4 other args: 5 >>> args_one('a', 'b', 'c', 'd', 'e') Normal arg: a other args: b other args: c other args: d other args: e >>> tuple_args = (1, 2, 3, 4, 5) >>> args_one(1, tuple_args) Normal arg: 1 other args: (1, 2, 3, 4, 5) >>> args_one(1, *tuple_args) Normal arg: 1 other args: 1 other args: 2 other args: 3 other args: 4 other args: 5 |
**kwargs表示关键字参数,它本质上是一个dict:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | >>> def args_two(narg, **kwargs): ... print("Normal arg:", narg) ... for key, value in kwargs.items(): ... print("other keyword arg: %s: %s" % (key, value)) ... >>> args_two(narg=1, karg1="one", karg2="two") Normal arg: 1 other keyword arg: karg1: one other keyword arg: karg2: two >>> key_args = {"a": "A", "b": "B", "c": "C"} # 以下两个是错误的调用示范 >>> args_two('a', key_args) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: args_two() takes 1 positional argument but 2 were given >>> args_two('a', *key_args) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: args_two() takes 1 positional argument but 4 were given # 正确做法 >>> args_two('a', **key_args) Normal arg: a other keyword arg: a: A other keyword arg: b: B other keyword arg: c: C |
同时使用*args和**kwargs时,必须*args参数列要在**kwargs前, 让我们最后来看一个综合例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | >>> def args_fun(normal, *args, **kwargs): ... print("narmal=", normal) ... print("*args=", args) ... print("**args=", kwargs) ... >>> args_fun(1) narmal= 1 *args= () **args= {} >>> args_fun(1, 2) narmal= 1 *args= (2,) **args= {} >>> args_fun(1, 2, 3) narmal= 1 *args= (2, 3) **args= {} >>> args_fun(1, a=1, b=2, c=3) narmal= 1 *args= () **args= {'a': 1, 'b': 2, 'c': 3} >>> args_fun(1, 2, 3, a=1, b=2, c=3) narmal= 1 *args= (2, 3) **args= {'a': 1, 'b': 2, 'c': 3} >>> args_fun(1, a=1, b=2, c=3, 2, 3) File "<stdin>", line 1 SyntaxError: positional argument follows keyword argument |
注:原创文章,转载请注明出处及保留链接“Python时代”:https://www.pythonage.com/
本文链接地址:Python Tip 2: python *args **kwargs 用法 https://www.pythonage.com/?p=15