Python中的*args和**kwargs

*args和**kwargs

*args**kwargs 主要用于函数定义。 你可以将不定数量的参数传递给一个函数。

这里的不定的意思是:预先并不知道, 函数使用者会传递多少个参数给你, 所以在这个场景下使用这两个关键字。

  • *args 是用来发送一个非键值对的可变数量的参数列表给一个函数.

    这里有个例子帮你理解这个概念:

1
2
3
4
5
6
def test_var_args(f_arg, *argv):
print("first normal arg:", f_arg)
for arg in argv:
print("another arg through *argv:", arg)

test_var_args('yasoob', 'python', 'eggs', 'test')

这会产生如下输出:

1
2
3
4
first normal arg: yasoob
another arg through *argv: python
another arg through *argv: eggs
another arg through *argv: test
  • **kwargs 允许你将不定长度的键值对, 作为参数传递给一个函数。 如果你想要在一个函数里处理带名字的参数, 你应该使用**kwargs

    这里有个让你上手的例子:

1
2
3
4
5
6
7
def greet_me(**kwargs):
for key, value in kwargs.items():
print("{0} == {1}".format(key, value))


>>> greet_me(name="yasoob")
name == yasoob

现在你可以看出我们怎样在一个函数里, 处理了一个键值对参数了。

这就是**kwargs的基础, 而且你可以看出它有多么管用。 接下来让我们谈谈,你怎样使用*args**kwargs来调用一个参数为列表或者字典的函数。

  • 使用*args和**kwargs来调用函数

    那现在我们将看到怎样使用*args**kwargs 来调用一个函数。 假设,你有这样一个小函数:

1
2
3
4
def test_args_kwargs(arg1, arg2, arg3):
print("arg1:", arg1)
print("arg2:", arg2)
print("arg3:", arg3)

你可以使用*args**kwargs来给这个小函数传递参数。 下面是怎样做:

1
2
3
4
5
6
7
8
9
10
11
12
13
# 首先使用 *args
>>> args = ("two", 3, 5)
>>> test_args_kwargs(*args)
arg1: two
arg2: 3
arg3: 5

# 现在使用 **kwargs:
>>> kwargs = {"arg3": 3, "arg2": "two", "arg1": 5}
>>> test_args_kwargs(**kwargs)
arg1: 5
arg2: two
arg3: 3
  • 标准参数与*args、**kwargs在使用时的顺序

    那么如果你想在函数里同时使用所有这三种参数, 顺序是这样的:

1
some_func(fargs, *args, **kwargs)
  • 何时使用?

    这需要根据需求而定。最常见的用例是在写函数装饰器的时候。

    此外它也可以用来做猴子补丁(monkey patching)。猴子补丁的意思是在程序运行时(runtime)修改某些代码。 打个比方,你有一个类,里面有个叫get_info的函数会调用一个API并返回相应的数据。如果我们想测试它,可以把API调用替换成一些测试数据。例如:

1
2
3
4
5
6
import someclass

def get_info(self, *args):
return "Test data"

someclass.get_info = get_info

文章内容来自:https://docs.pythontab.com/interpy/args_kwargs/When_to_use/