Python中的**kwargs有什么用途?

我知道你可以在表上执行objects.filter并传递一个**kwargs参数。 

我也可以这样做来指定时间增量,即时间增量(小时=时间1)吗?

它到底是如何工作的?是否归类为“开箱”?比如a,b=1,2?


当前回答

正在解压缩词典

**拆开词典。

This

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)

函数的包装参数

对python 3使用.items()而不是.iteritems()

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和args。


资源

PEP 448:其他解包概括真实Python:Python args和kwargs:解密在函数签名中,变量名前的*和**意味着什么?

其他回答

正在解压缩词典

**拆开词典。

This

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)

函数的包装参数

对python 3使用.items()而不是.iteritems()

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和args。


资源

PEP 448:其他解包概括真实Python:Python args和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函数的签名中。

在这里,您可以将夸rgs放入游戏中——只需将夸rg斯添加到签名中即可:

def myDo(what, where, why, **kwargs):
   if what == 'drive':
      doDrive(where, why, **kwargs)
   elif what == 'swim':
      doSwim(where, why, **kwargs)

这样,您就不需要在每次调用的某些例程发生更改时更改接口函数的签名。

这只是一个很好的例子,你会发现夸尔格很有用。

下面是一个我希望有帮助的例子:

#! /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

这里的关键是调用中命名参数的可变数量转换为函数中的字典。

此外,您还可以在调用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必须是最后一个参数

在Java中,使用构造函数重载类并允许多个输入参数。在python中,可以使用kwargs来提供类似的行为。

java示例:https://beginnersbook.com/2013/05/constructor-overloading/

python示例:

class Robot():
    # name is an arg and color is a kwarg
    def __init__(self,name, color='red'):
        self.name = name
        self.color = color

red_robot = Robot('Bob')
blue_robot = Robot('Bob', color='blue')

print("I am a {color} robot named {name}.".format(color=red_robot.color, name=red_robot.name))
print("I am a {color} robot named {name}.".format(color=blue_robot.color, name=blue_robot.name))

>>> I am a red robot named Bob.
>>> I am a blue robot named Bob.

只是另一种思考方式。