我发现以下链接对解释非常有帮助*
,*args
并且**kwargs
:
https://pythontips.com/2013/08/04/args-and-kwargs-in-python-explained/
基本上,除了上面的答案之外,我还从上面的网站(信用:https ://pythontips.com/author/yasoob008/ )了解到以下内容:
下面先定义演示函数,有两个例子,一个with *args
,一个with**kwargs
def test_args_kwargs(arg1, arg2, arg3):
print "arg1:", arg1
print "arg2:", arg2
print "arg3:", arg3
# first with *args
>>> args = ("two", 3,5)
>>> test_args_kwargs(*args)
arg1: two
arg2: 3
arg3: 5
# now with **kwargs:
>>> kwargs = {"arg3": 3, "arg2": "two","arg1":5}
>>> test_args_kwargs(**kwargs)
arg1: 5
arg2: two
arg3: 3
因此*args
,您可以动态构建参数列表,这些参数将按输入顺序进行,同时**kwargs
可以启用 NAMED 参数的传递,并且可以由 NAME 相应地处理(与输入顺序无关) .
该网站继续指出,参数的正确顺序应该是:
some_func(fargs,*args,**kwargs)