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

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

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

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


当前回答

在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.

只是另一种思考方式。

其他回答

kwargs是一种语法糖,可以将名称参数作为字典传递(对于func),或将字典作为命名参数传递(对于func)

关键字Arguments在Python中通常缩写为kwargs。在计算机编程中,

关键字参数是指计算机语言对函数的支持调用,明确说明函数调用。

在参数名**kwargs之前使用两个星号是因为不知道有多少关键字参数将传递到函数中。在这种情况下,它被称为任意/通配符关键字参数。

Django的接收器函数就是一个例子。

def my_callback(sender, **kwargs):
    print("Request finished!")

注意,该函数接受sender参数和通配符关键字参数(**kwargs);所有信号处理程序都必须接受这些论据。所有信号都发送关键字参数,并可能更改这些参数关键字参数。在request_finished的情况下记录为不发送参数,这意味着我们可能会将我们的信号处理写成mycallback(sender)。这是错误的——事实上,如果你这样做,Django会抛出错误这是因为在任何时候,参数都可以添加到信号和接收器必须能够处理这些新参数。

注意,它不必被称为kwargs,但它需要有**(kwargs这个名称是一个惯例)。

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

下面是一个简单的函数,用于解释用法:

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

在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.

只是另一种思考方式。