**kwargs
在Python中有什么用途?
我知道你可以在桌子上做一个objects.filter
并传入**kwargs
论点。
我也可以这样做来指定时间增量,即timedelta(hours = time1)
吗?
它究竟是如何工作的?这类是“解包”吗?像a,b=1,2
?
您可以使用**kwargs
让您的函数采用任意数量的关键字参数(“kwargs”表示“关键字参数”):
>>> def print_keyword_args(**kwargs):
... # kwargs is a dict of the keyword args passed to the function
... for key, value in kwargs.iteritems():
... print "%s = %s" % (key, value)
...
>>> print_keyword_args(first_name="John", last_name="Doe")
first_name = John
last_name = Doe
在调用函数时,您还可以通过构造关键字参数字典并将其传递给函数来使用**kwargs
语法:
>>> kwargs = {'first_name': 'Bobby', 'last_name': 'Smith'}
>>> print_keyword_args(**kwargs)
first_name = Bobby
last_name = Smith
Python Tutorial包含一个很好的解释它的工作方式,以及一些很好的例子。
< - 更新 - >
对于使用Python 3的人而不是iteritems(),使用items()
这是一个我希望有用的例子:
#! /usr/bin/env python
#
def g( **kwargs) :
print ( "In g ready to print kwargs" )
print kwargs
print ( "in g, calling f")
f ( **kwargs )
print ( "In g, after returning from f")
def f( **kwargs ) :
print ( "in f, printing kwargs")
print ( kwargs )
print ( "In f, after printing kwargs")
g( a="red", b=5, c="Nassau")
g( q="purple", w="W", c="Charlie", d=[4, 3, 6] )
运行程序时,您会得到:
$ python kwargs_demo.py
In g ready to print kwargs
{'a': 'red', 'c': 'Nassau', 'b': 5}
in g, calling f
in f, printing kwargs
{'a': 'red', 'c': 'Nassau', 'b': 5}
In f, after printing kwargs
In g, after returning from f
In g ready to print kwargs
{'q': 'purple', 'c': 'Charlie', 'd': [4, 3, 6], 'w': 'W'}
in g, calling f
in f, printing kwargs
{'q': 'purple', 'c': 'Charlie', 'd': [4, 3, 6], 'w': 'W'}
In f, after printing kwargs
In g, after returning from f
这里的关键是调用中可变数量的命名参数转换为函数中的字典。
这是了解python解包的简单示例,
>>> def f(*args, **kwargs):
... print 'args', args, 'kwargs', kwargs
点拨:
>>>f(1, 2)
>>> args (1,2) kwargs {} #args return parameter without reference as a tuple
>>>f(a = 1, b = 2)
>>> args () kwargs {'a': 1, 'b': 2} #args is empty tuple and kwargs return parameter with reference as a dictionary
**
解压缩字典。
这个
func(a=1, b=2, c=3)
是相同的
args = {'a': 1, 'b': 2, 'c':3}
func(**args)
如果必须构造参数,这很有用:
args = {'name': person.name}
if hasattr(person, "address"):
args["address"] = person.address
func(**args) # either expanded to func(name=person.name) or
# func(name=person.name, address=person.address)
def setstyle(**styles):
for key, value in styles.iteritems(): # styles is a regular dictionary
setattr(someobject, key, value)
这可以让你使用这样的功能:
setstyle(color="red", bold=False)
kwargs只是一个添加到参数中的字典。
字典可以包含键值对。这就是kwargs。好的,这是怎么回事。
什么不是那么简单。
例如(非常假设)你有一个接口只调用其他例程来完成这项工作:
def myDo(what, where, why):
if what == 'swim':
doSwim(where, why)
elif what == 'walk':
doWalk(where, why)
...
现在你得到一个新的方法“驱动器”:
elif what == 'drive':
doDrive(where, why, vehicle)
但是等一下,有一个新的参数“车辆” - 你以前不知道。现在您必须将其添加到myDo功能的签名中。
在这里,您可以将kwargs投入游戏 - 您只需将kwargs添加到签名中:
def myDo(what, where, why, **kwargs):
if what == 'drive':
doDrive(where, why, **kwargs)
elif what == 'swim':
doSwim(where, why, **kwargs)
这样,每次某些被调用的例程可能会发生变化时,您无需更改接口函数的签名。
这只是一个很好的例子,你可以找到有用的kwargs。
基于良好的样本有时候比长话语更好,我将使用所有python变量参数传递工具(位置和命名参数)编写两个函数。您应该可以轻松地自己查看它的功能:
def f(a = 0, *args, **kwargs):
print("Received by f(a, *args, **kwargs)")
print("=> f(a=%s, args=%s, kwargs=%s" % (a, args, kwargs))
print("Calling g(10, 11, 12, *args, d = 13, e = 14, **kwargs)")
g(10, 11, 12, *args, d = 13, e = 14, **kwargs)
def g(f, g = 0, *args, **kwargs):
print("Received by g(f, g = 0, *args, **kwargs)")
print("=> g(f=%s, g=%s, args=%s, kwargs=%s)" % (f, g, args, kwargs))
print("Calling f(1, 2, 3, 4, b = 5, c = 6)")
f(1, 2, 3, 4, b = 5, c = 6)
这是输出:
Calling f(1, 2, 3, 4, b = 5, c = 6)
Received by f(a, *args, **kwargs)
=> f(a=1, args=(2, 3, 4), kwargs={'c': 6, 'b': 5}
Calling g(10, 11, 12, *args, d = 13, e = 14, **kwargs)
Received by g(f, g = 0, *args, **kwargs)
=> g(f=10, g=11, args=(12, 2, 3, 4), kwargs={'c': 6, 'b': 5, 'e': 14, 'd': 13})
Motif:*args
和**kwargs
用作需要传递给函数调用的参数的占位符
使用*args
和**kwargs
来调用函数
def args_kwargs_test(arg1, arg2, arg3):
print "arg1:", arg1
print "arg2:", arg2
print "arg3:", arg3
现在我们将使用*args
来调用上面定义的函数
#args can either be a "list" or "tuple"
>>> args = ("two", 3, 5)
>>> args_kwargs_test(*args)
arg1:两个 arg2:3 arg3:5
现在,使用**kwargs
调用相同的函数
#keyword argument "kwargs" has to be a dictionary
>>> kwargs = {"arg3":3, "arg2":'two', "arg1":5}
>>> args_kwargs_test(**kwargs)
arg1:5 arg2:两个 arg3:3
底线:*args
没有智能,它只是简单地将传递的参数插入参数(按从左到右的顺序),而**kwargs
通过在所需位置放置适当的值来智能地表现
kwargs
的**kwargs
只是变量名。你可以很好地拥有**anyVariableName
kwargs
代表“关键字参数”。但我认为最好将它们称为“命名参数”,因为这些只是与名称一起传递的参数(我在“关键字参数”一词中找不到“关键字”这个词的任何意义。我猜“关键字”通常意味着由编程语言保留的单词,因此不被程序员用于变量名。在kwargs的情况下,这里不会发生这样的事情。)因此,我们将名称param1
和param2
赋予传递给函数的两个参数值,如下所示:func(param1="val1",param2="val2")
,而不是仅传递值:func(val1,val2)
。因此,我觉得他们应该适当地称为“任意数量的命名参数”,因为我们可以指定任意数量的这些参数(即参数),如果func
有签名func(**kwargs)
所以说,让我首先解释“命名参数”,然后“任意数量的命名参数”kwargs
。
命名参数
def function1(param1,param2="arg2",param3="arg3"):
print("\n"+str(param1)+" "+str(param2)+" "+str(param3)+"\n")
function1(1) #1 arg2 arg3 #1 positional arg
function1(param1=1) #1 arg2 arg3 #1 named arg
function1(1,param2=2) #1 2 arg3 #1 positional arg, 1 named arg
function1(param1=1,param2=2) #1 2 arg3 #2 named args
function1(param2=2, param1=1) #1 2 arg3 #2 named args out of order
function1(1, param3=3, param2=2) #1 2 3 #
#function1() #invalid: required argument missing
#function1(param2=2,1) #invalid: SyntaxError: non-keyword arg after keyword arg
#function1(1,param1=11) #invalid: TypeError: function1() got multiple values for argument 'param1'
#function1(param4=4) #invalid: TypeError: function1() got an unexpected keyword argument 'param4'
任意数量的命名参数kwargs
def function2(param1, *tupleParams, param2, param3, **dictionaryParams):
print("param1: "+ param1)
print("param2: "+ param2)
print("param3: "+ param3)
print("custom tuple params","-"*10)
for p in tupleParams:
print(str(p) + ",")
print("custom named params","-"*10)
for k,v in dictionaryParams.items():
print(str(k)+":"+str(v))
function2("arg1",
"custom param1",
"custom param2",
"custom param3",
param3="arg3",
param2="arg2",
customNamedParam1 = "val1",
customNamedParam2 = "val2"
)
# Output
#
#param1: arg1
#param2: arg2
#param3: arg3
#custom tuple params ----------
#custom param1,
#custom param2,
#custom param3,
#custom named params ----------
#customNamedParam2:val2
#customNamedParam1:val1
传递自定义args的元组和字典变量
为了完成它,我还要注意,我们可以通过
因此,上述相同的调用可以如下进行:
tupleCustomArgs = ("custom param1", "custom param2", "custom param3")
dictCustomNamedArgs = {"customNamedParam1":"val1", "customNamedParam2":"val2"}
function2("arg1",
*tupleCustomArgs, #note *
param3="arg3",
param2="arg2",
**dictCustomNamedArgs #note **
)
最后请注意上面函数调用中的*
和**
。如果我们省略它们,我们可能会得到不好的结果。
在元组中省略*
:
function2("arg1",
tupleCustomArgs, #omitting *
param3="arg3",
param2="arg2",
**dictCustomNamedArgs
)
版画
param1: arg1
param2: arg2
param3: arg3
custom tuple params ----------
('custom param1', 'custom param2', 'custom param3'),
custom named params ----------
customNamedParam2:val2
customNamedParam1:val1
上面的元组('custom param1', 'custom param2', 'custom param3')
按原样打印。
省略dict
args:
function2("arg1",
*tupleCustomArgs,
param3="arg3",
param2="arg2",
dictCustomNamedArgs #omitting **
)
给
dictCustomNamedArgs
^
SyntaxError: non-keyword arg after keyword arg
另外,在调用kwargs函数时,您还可以混合使用不同的方法:
def test(**kwargs):
print kwargs['a']
print kwargs['b']
print kwargs['c']
args = { 'b': 2, 'c': 3}
test( a=1, **args )
给出这个输出:
1
2
3
请注意,** kwargs必须是最后一个参数
kwargs是一个语法糖,用于将名称参数作为字典(用于func)或字典作为命名参数(用于func)
这是一个简单的函数,用于解释用法:
def print_wrap(arg1, *args, **kwargs):
print(arg1)
print(args)
print(kwargs)
print(arg1, *args, **kwargs)
任何未在函数定义中指定的参数都将放在args
列表或kwargs
列表中,具体取决于它们是否为关键字参数:
>>> print_wrap('one', 'two', 'three', end='blah', sep='--')
one
('two', 'three')
{'end': 'blah', 'sep': '--'}
one--two--threeblah
如果添加一个永远不会传递给函数的关键字参数,则会引发错误:
>>> print_wrap('blah', dead_arg='anything')
TypeError: 'dead_arg' is an invalid keyword argument for this function