标签归档:kwargs

传递名单清单?

问题:传递名单清单?

我可以将简短的清单传递给一种简洁的方法吗?这是我正在尝试做的事情:

def method(**kwargs):
    #do something

keywords = (keyword1 = 'foo', keyword2 = 'bar')
method(keywords)

Can I pass a list of kwargs to a method for brevity? This is what i’m attempting to do:

def method(**kwargs):
    #do something

keywords = (keyword1 = 'foo', keyword2 = 'bar')
method(keywords)

回答 0

是。您可以这样操作:

def method(**kwargs):
  print kwargs

keywords = {'keyword1': 'foo', 'keyword2': 'bar'}
method(keyword1='foo', keyword2='bar')
method(**keywords)

在Python中运行此命令可以确认产生相同的结果:

{'keyword2': 'bar', 'keyword1': 'foo'}
{'keyword2': 'bar', 'keyword1': 'foo'}

Yes. You do it like this:

def method(**kwargs):
  print kwargs

keywords = {'keyword1': 'foo', 'keyword2': 'bar'}
method(keyword1='foo', keyword2='bar')
method(**keywords)

Running this in Python confirms these produce identical results:

{'keyword2': 'bar', 'keyword1': 'foo'}
{'keyword2': 'bar', 'keyword1': 'foo'}

回答 1

正如其他人指出的那样,您可以通过传递命令来做您想做的事情。有多种方法可以构建字典。保留keyword=value您尝试的样式的一种方法是使用内置的dict

keywords = dict(keyword1 = 'foo', keyword2 = 'bar')

注意的多功能性dict; 所有这些都会产生相同的结果:

>>> kw1 = dict(keyword1 = 'foo', keyword2 = 'bar')
>>> kw2 = dict({'keyword1':'foo', 'keyword2':'bar'})
>>> kw3 = dict([['keyword1', 'foo'], ['keyword2', 'bar']])
>>> kw4 = dict(zip(('keyword1', 'keyword2'), ('foo', 'bar')))
>>> assert kw1 == kw2 == kw3 == kw4
>>> 

As others have pointed out, you can do what you want by passing a dict. There are various ways to construct a dict. One that preserves the keyword=value style you attempted is to use the dict built-in:

keywords = dict(keyword1 = 'foo', keyword2 = 'bar')

Note the versatility of dict; all of these produce the same result:

>>> kw1 = dict(keyword1 = 'foo', keyword2 = 'bar')
>>> kw2 = dict({'keyword1':'foo', 'keyword2':'bar'})
>>> kw3 = dict([['keyword1', 'foo'], ['keyword2', 'bar']])
>>> kw4 = dict(zip(('keyword1', 'keyword2'), ('foo', 'bar')))
>>> assert kw1 == kw2 == kw3 == kw4
>>> 

回答 2

你是说一个命令吗?你当然可以:

def method(**kwargs):
    #do something

keywords = {keyword1: 'foo', keyword2: 'bar'}
method(**keywords)

Do you mean a dict? Sure you can:

def method(**kwargs):
    #do something

keywords = {keyword1: 'foo', keyword2: 'bar'}
method(**keywords)

回答 3

因此,当我来到这里时,我正在寻找一种在一个函数中传递多个** kwarg的方法-供以后在其他函数中使用。因为这并不奇怪,所以不起作用:

def func1(**f2_x, **f3_x):
     ...

通过一些自己的“实验”编码,我得出了显而易见的方法:

def func3(f3_a, f3_b):
    print "--func3--"
    print f3_a
    print f3_b
def func2(f2_a, f2_b):
    print "--func2--"
    print f2_a
    print f2_b

def func1(f1_a, f1_b, f2_x={},f3_x={}):
    print "--func1--"
    print f1_a
    print f1_b
    func2(**f2_x)
    func3(**f3_x)

func1('aaaa', 'bbbb', {'f2_a':1, 'f2_b':2}, {'f3_a':37, 'f3_b':69})

按预期打印:

--func1--
aaaa
bbbb
--func2--
1
2
--func3--
37
69

So when I’ve come here I was looking for a way to pass several **kwargs in one function – for later use in further functions. Because this, not that surprisingly, doesn’t work:

def func1(**f2_x, **f3_x):
     ...

With some own ‘experimental’ coding I came to the obviously way how to do it:

def func3(f3_a, f3_b):
    print "--func3--"
    print f3_a
    print f3_b
def func2(f2_a, f2_b):
    print "--func2--"
    print f2_a
    print f2_b

def func1(f1_a, f1_b, f2_x={},f3_x={}):
    print "--func1--"
    print f1_a
    print f1_b
    func2(**f2_x)
    func3(**f3_x)

func1('aaaa', 'bbbb', {'f2_a':1, 'f2_b':2}, {'f3_a':37, 'f3_b':69})

This prints as expected:

--func1--
aaaa
bbbb
--func2--
1
2
--func3--
37
69

如何检查** kwargs键是否存在?

问题:如何检查** kwargs键是否存在?

Python 3.2.3。这里列出了一些想法,这些想法可以在常规var上使用,但是** kwargs似乎遵循不同的规则…所以,为什么这行不通?我如何检查** kwargs中的键是否存在?

if kwargs['errormessage']:
    print("It exists")

我也认为这应该可行,但是不-

if errormessage in kwargs:
    print("yeah it's here")

我猜是因为kwargs是可迭代的?我是否必须遍历它只是为了检查是否存在特定的密钥?

Python 3.2.3. There were some ideas listed here, which work on regular var’s, but it seems **kwargs play by different rules… so why doesn’t this work and how can I check to see if a key in **kwargs exists?

if kwargs['errormessage']:
    print("It exists")

I also think this should work, but it doesn’t —

if errormessage in kwargs:
    print("yeah it's here")

I’m guessing because kwargs is iterable? Do I have to iterate through it just to check if a particular key is there?


回答 0

你要

if 'errormessage' in kwargs:
    print("found it")

获得价值 errormessage

if 'errormessage' in kwargs:
    print("errormessage equals " + kwargs.get("errormessage"))

这样,kwargs只是另一个dict。您的第一个示例,if kwargs['errormessage']意思是“获取与kwargs中的键“ errormessage”关联的值,然后检查其bool值”。因此,如果没有这样的密钥,您将得到一个KeyError

您的第二个示例if errormessage in kwargs:表示“如果kwargs包含以”命名的元素errormessage,并且除非“ errormessage”是变量的名称,否则您将获得NameError

我应该提到的是,字典也有一个.get()接受默认参数的方法(本身默认为None),以便kwargs.get("errormessage")在该键存在时返回值,None否则返回值(类似地kwargs.get("errormessage", 17),您可能会认为这样做)。当您不关心键的存在与拥有None值或键不存在之间的区别时,这会很方便。

You want

if 'errormessage' in kwargs:
    print("found it")

To get the value of errormessage

if 'errormessage' in kwargs:
    print("errormessage equals " + kwargs.get("errormessage"))

In this way, kwargs is just another dict. Your first example, if kwargs['errormessage'], means “get the value associated with the key “errormessage” in kwargs, and then check its bool value”. So if there’s no such key, you’ll get a KeyError.

Your second example, if errormessage in kwargs:, means “if kwargs contains the element named by “errormessage“, and unless “errormessage” is the name of a variable, you’ll get a NameError.

I should mention that dictionaries also have a method .get() which accepts a default parameter (itself defaulting to None), so that kwargs.get("errormessage") returns the value if that key exists and None otherwise (similarly kwargs.get("errormessage", 17) does what you might think it does). When you don’t care about the difference between the key existing and having None as a value or the key not existing, this can be handy.


回答 1

DSM和Tadeck的答案将直接回答您的问题。

在我的脚本中,我经常使用便捷dict.pop()来处理可选参数和其他参数。这是一个简单print()包装器的示例:

def my_print(*args, **kwargs):
    prefix = kwargs.pop('prefix', '')
    print(prefix, *args, **kwargs)

然后:

>>> my_print('eggs')
 eggs
>>> my_print('eggs', prefix='spam')
spam eggs

如您所见,如果prefix中未包含kwargs,则默认值''(空字符串)存储在本地prefix变量中。如果给出,则使用其值。

对于编写任何类型的函数的包装程序,通常这是一个紧凑且易读的方法:始终只是传递您不了解的传递参数,甚至不知道它们是否存在。如果您总是通过,*args并且**kwargs使代码变慢,并且需要更多键入,但是如果被调用函数的接口(在这种情况下print)发生了变化,则无需更改代码。这种方法减少了开发时间,同时支持所有接口更改。

DSM’s and Tadeck’s answers answer your question directly.

In my scripts I often use the convenient dict.pop() to deal with optional, and additional arguments. Here’s an example of a simple print() wrapper:

def my_print(*args, **kwargs):
    prefix = kwargs.pop('prefix', '')
    print(prefix, *args, **kwargs)

Then:

>>> my_print('eggs')
 eggs
>>> my_print('eggs', prefix='spam')
spam eggs

As you can see, if prefix is not contained in kwargs, then the default '' (empty string) is being stored in the local prefix variable. If it is given, then its value is being used.

This is generally a compact and readable recipe for writing wrappers for any kind of function: Always just pass-through arguments you don’t understand, and don’t even know if they exist. If you always pass through *args and **kwargs you make your code slower, and requires a bit more typing, but if interfaces of the called function (in this case print) changes, you don’t need to change your code. This approach reduces development time while supporting all interface changes.


回答 2

就是这样:

if 'errormessage' in kwargs:
    print("yeah it's here")

您需要检查密钥是否在字典中。这样做的语法是some_key in some_dict(这里some_key是可哈希的,不一定是字符串)。

您链接的想法(这些想法)包含用于检查locals()and所返回的字典中是否存在特定键的示例globals()。您的示例与此类似,因为您正在检查kwargs字典(包含关键字参数的字典)中是否存在特定键。

It is just this:

if 'errormessage' in kwargs:
    print("yeah it's here")

You need to check, if the key is in the dictionary. The syntax for that is some_key in some_dict (where some_key is something hashable, not necessarily a string).

The ideas you have linked (these ideas) contained examples for checking if specific key existed in dictionaries returned by locals() and globals(). Your example is similar, because you are checking existence of specific key in kwargs dictionary (the dictionary containing keyword arguments).


回答 3

一种方法是自己添加!怎么样?通过合并kwargs一堆默认值。例如,如果您事先不知道密钥,则此方法不适用于所有情况。但是,如果有,这是一个简单的示例:

import sys

def myfunc(**kwargs):
    args = {'country':'England','town':'London',
            'currency':'Pound', 'language':'English'}

    diff = set(kwargs.keys()) - set(args.keys())
    if diff:
        print("Invalid args:",tuple(diff),file=sys.stderr)
        return

    args.update(kwargs)            
    print(args)

默认值在字典中设置,该字典args包括我们期望的所有键。我们首先检查kwarg中是否有任何意外的键。然后我们更新argskwargs,这将覆盖任何新的价值用户已设置。我们不需要测试键是否存在,我们现在将其args用作参数字典,并且不再需要kwargs

One way is to add it by yourself! How? By merging kwargs with a bunch of defaults. This won’t be appropriate on all occasions, for example, if the keys are not known to you in advance. However, if they are, here is a simple example:

import sys

def myfunc(**kwargs):
    args = {'country':'England','town':'London',
            'currency':'Pound', 'language':'English'}

    diff = set(kwargs.keys()) - set(args.keys())
    if diff:
        print("Invalid args:",tuple(diff),file=sys.stderr)
        return

    args.update(kwargs)            
    print(args)

The defaults are set in the dictionary args, which includes all the keys we are expecting. We first check to see if there are any unexpected keys in kwargs. Then we update args with kwargs which will overwrite any new values that the user has set. We don’t need to test if a key exists, we now use args as our argument dictionary and have no further need of kwargs.


回答 4

您可以自己轻松发现这些东西:

def hello(*args, **kwargs):
    print kwargs
    print type(kwargs)
    print dir(kwargs)

hello(what="world")

You can discover those things easily by yourself:

def hello(*args, **kwargs):
    print kwargs
    print type(kwargs)
    print dir(kwargs)

hello(what="world")

回答 5

if kwarg.__len__() != 0:
    print(kwarg)
if kwarg.__len__() != 0:
    print(kwarg)

将Python字典转换为kwargs?

问题:将Python字典转换为kwargs?

我想使用类继承构建一个针对sunburnt(solr interface)的查询,因此将键-值对加在一起。sunburnt接口带有关键字参数。如何将字典({'type':'Event'})转换为关键字参数(type='Event')

I want to build a query for sunburnt(solr interface) using class inheritance and therefore adding key – value pairs together. The sunburnt interface takes keyword arguments. How can I transform a dict ({'type':'Event'}) into keyword arguments (type='Event')?


回答 0

使用双星运算符(又名double-splat?):

func(**{'type':'Event'})

相当于

func(type='Event')

Use the double-star (aka double-splat?) operator:

func(**{'type':'Event'})

is equivalent to

func(type='Event')

回答 1

** 操作员在这里会有所帮助。

**操作员将解开dict元素的包装,因此**{'type':'Event'}将被视为type='Event'

func(**{'type':'Event'}) 与…相同 func(type='Event') dict元素将转换为相同keyword arguments

费耶

* 将解压缩列表元素,它们将被视为 positional arguments

func(*['one', 'two']) 与…相同 func('one', 'two')

** operator would be helpful here.

** operator will unpack the dict elements and thus **{'type':'Event'} would be treated as type='Event'

func(**{'type':'Event'}) is same as func(type='Event') i.e the dict elements would be converted to the keyword arguments.

FYI

* will unpack the list elements and they would be treated as positional arguments.

func(*['one', 'two']) is same as func('one', 'two')


回答 2

这是一个完整的示例,显示了如何使用**运算符将字典中的值作为关键字参数传递。

>>> def f(x=2):
...     print(x)
... 
>>> new_x = {'x': 4}
>>> f()        #    default value x=2
2
>>> f(x=3)     #   explicit value x=3
3
>>> f(**new_x) # dictionary value x=4 
4

Here is a complete example showing how to use the ** operator to pass values from a dictionary as keyword arguments.

>>> def f(x=2):
...     print(x)
... 
>>> new_x = {'x': 4}
>>> f()        #    default value x=2
2
>>> f(x=3)     #   explicit value x=3
3
>>> f(**new_x) # dictionary value x=4 
4

在Python中使用** kwargs的正确方法

问题:在Python中使用** kwargs的正确方法

关于**kwargs默认值,在Python中使用的正确方法是什么?

kwargs返回一本字典,但是设置默认值的最佳方法是什么?我应该以字典的形式访问它吗?使用获取功能?

class ExampleClass:
    def __init__(self, **kwargs):
        self.val = kwargs['val']
        self.val2 = kwargs.get('val2')

一个简单的问题,但是我找不到很好的资源。人们在我见过的代码中以不同的方式进行操作,很难知道该使用什么。

What is the proper way to use **kwargs in Python when it comes to default values?

kwargs returns a dictionary, but what is the best way to set default values, or is there one? Should I just access it as a dictionary? Use get function?

class ExampleClass:
    def __init__(self, **kwargs):
        self.val = kwargs['val']
        self.val2 = kwargs.get('val2')

A simple question, but one that I can’t find good resources on. People do it different ways in code that I’ve seen and it’s hard to know what to use.


回答 0

您可以将默认值传递给get()不在字典中的键:

self.val2 = kwargs.get('val2',"default value")

但是,如果计划使用具有特定默认值的特定参数,为什么不首先使用命名参数?

def __init__(self, val2="default value", **kwargs):

You can pass a default value to get() for keys that are not in the dictionary:

self.val2 = kwargs.get('val2',"default value")

However, if you plan on using a particular argument with a particular default value, why not use named arguments in the first place?

def __init__(self, val2="default value", **kwargs):

回答 1

虽然大多数答案都在说,例如,

def f(**kwargs):
    foo = kwargs.pop('foo')
    bar = kwargs.pop('bar')
    ...etc...

是相同的”

def f(foo=None, bar=None, **kwargs):
    ...etc...

这不是真的。在后一种情况下,f可以称为f(23, 42),而前一种情况接受命名参数-不能进行位置调用。通常,您想让调用者具有最大的灵活性,因此,如大多数答案所断言的那样,第二种形式是可取的:但这并非总是如此。当您接受许多通常仅传递几个可选参数的可选参数时,强制使用命名参数可能是一个好主意(避免意外和调用代码不可读!)threading.Thread就是一个例子。第一种形式是如何在Python 2中实现它。

成语是如此重要,以至于在Python 3现在有专门的配套语法:单后每个参数*def签名关键字只,也就是说,不能被作为位置参数传递,但只是作为一个命名的。因此,在Python 3中,您可以将上述代码编写为:

def f(*, foo=None, bar=None, **kwargs):
    ...etc...

实际上,在Python 3中,您甚至可以具有可选的仅关键字参数(没有默认值的参数)。

但是,Python 2仍有很长的生产寿命,因此最好不要忘记使您能够在Python 2中实现重要设计思想的技术和习惯用法,而Python 3语言直接支持这些重要设计思想!

While most answers are saying that, e.g.,

def f(**kwargs):
    foo = kwargs.pop('foo')
    bar = kwargs.pop('bar')
    ...etc...

is “the same as”

def f(foo=None, bar=None, **kwargs):
    ...etc...

this is not true. In the latter case, f can be called as f(23, 42), while the former case accepts named arguments only — no positional calls. Often you want to allow the caller maximum flexibility and therefore the second form, as most answers assert, is preferable: but that is not always the case. When you accept many optional parameters of which typically only a few are passed, it may be an excellent idea (avoiding accidents and unreadable code at your call sites!) to force the use of named arguments — threading.Thread is an example. The first form is how you implement that in Python 2.

The idiom is so important that in Python 3 it now has special supporting syntax: every argument after a single * in the def signature is keyword-only, that is, cannot be passed as a positional argument, but only as a named one. So in Python 3 you could code the above as:

def f(*, foo=None, bar=None, **kwargs):
    ...etc...

Indeed, in Python 3 you can even have keyword-only arguments that aren’t optional (ones without a default value).

However, Python 2 still has long years of productive life ahead, so it’s better to not forget the techniques and idioms that let you implement in Python 2 important design ideas that are directly supported in the language in Python 3!


回答 2

我建议像这样

def testFunc( **kwargs ):
    options = {
            'option1' : 'default_value1',
            'option2' : 'default_value2',
            'option3' : 'default_value3', }

    options.update(kwargs)
    print options

testFunc( option1='new_value1', option3='new_value3' )
# {'option2': 'default_value2', 'option3': 'new_value3', 'option1': 'new_value1'}

testFunc( option2='new_value2' )
# {'option1': 'default_value1', 'option3': 'default_value3', 'option2': 'new_value2'}

然后根据需要使用值

dictionaryA.update(dictionaryB)添加的内容dictionaryBdictionaryA覆盖任何重复的密钥。

I suggest something like this

def testFunc( **kwargs ):
    options = {
            'option1' : 'default_value1',
            'option2' : 'default_value2',
            'option3' : 'default_value3', }

    options.update(kwargs)
    print options

testFunc( option1='new_value1', option3='new_value3' )
# {'option2': 'default_value2', 'option3': 'new_value3', 'option1': 'new_value1'}

testFunc( option2='new_value2' )
# {'option1': 'default_value1', 'option3': 'default_value3', 'option2': 'new_value2'}

And then use the values any way you want

dictionaryA.update(dictionaryB) adds the contents of dictionaryB to dictionaryA overwriting any duplicate keys.


回答 3

你会的

self.attribute = kwargs.pop('name', default_value)

要么

self.attribute = kwargs.get('name', default_value)

如果使用pop,则可以检查是否发送了任何虚假值,并采取适当的措施(如果有)。

You’d do

self.attribute = kwargs.pop('name', default_value)

or

self.attribute = kwargs.get('name', default_value)

If you use pop, then you can check if there are any spurious values sent, and take the appropriate action (if any).


回答 4

使用** kwargs和默认值很容易。但是,有时候,您不应该一开始就使用** kwargs。

在这种情况下,我们并没有真正充分利用** kwargs。

class ExampleClass( object ):
    def __init__(self, **kwargs):
        self.val = kwargs.get('val',"default1")
        self.val2 = kwargs.get('val2',"default2")

以上是“为什么要打扰?” 宣言。与…相同

class ExampleClass( object ):
    def __init__(self, val="default1", val2="default2"):
        self.val = val
        self.val2 = val2

当您使用** kwargs时,意味着关键字不仅是可选的,而且是有条件的。规则比简单的默认值复杂得多。

当您使用** kwargs时,通常意味着类似以下内容,其中简单的默认值不适用。

class ExampleClass( object ):
    def __init__(self, **kwargs):
        self.val = "default1"
        self.val2 = "default2"
        if "val" in kwargs:
            self.val = kwargs["val"]
            self.val2 = 2*self.val
        elif "val2" in kwargs:
            self.val2 = kwargs["val2"]
            self.val = self.val2 / 2
        else:
            raise TypeError( "must provide val= or val2= parameter values" )

Using **kwargs and default values is easy. Sometimes, however, you shouldn’t be using **kwargs in the first place.

In this case, we’re not really making best use of **kwargs.

class ExampleClass( object ):
    def __init__(self, **kwargs):
        self.val = kwargs.get('val',"default1")
        self.val2 = kwargs.get('val2',"default2")

The above is a “why bother?” declaration. It is the same as

class ExampleClass( object ):
    def __init__(self, val="default1", val2="default2"):
        self.val = val
        self.val2 = val2

When you’re using **kwargs, you mean that a keyword is not just optional, but conditional. There are more complex rules than simple default values.

When you’re using **kwargs, you usually mean something more like the following, where simple defaults don’t apply.

class ExampleClass( object ):
    def __init__(self, **kwargs):
        self.val = "default1"
        self.val2 = "default2"
        if "val" in kwargs:
            self.val = kwargs["val"]
            self.val2 = 2*self.val
        elif "val2" in kwargs:
            self.val2 = kwargs["val2"]
            self.val = self.val2 / 2
        else:
            raise TypeError( "must provide val= or val2= parameter values" )

回答 5

由于**kwargs在参数个数未知时使用,为什么不这样做呢?

class Exampleclass(object):
  def __init__(self, **kwargs):
    for k in kwargs.keys():
       if k in [acceptable_keys_list]:
          self.__setattr__(k, kwargs[k])

Since **kwargs is used when the number of arguments is unknown, why not doing this?

class Exampleclass(object):
  def __init__(self, **kwargs):
    for k in kwargs.keys():
       if k in [acceptable_keys_list]:
          self.__setattr__(k, kwargs[k])

回答 6

这是另一种方法:

def my_func(arg1, arg2, arg3):
    ... so something ...

kwargs = {'arg1': 'Value One', 'arg2': 'Value Two', 'arg3': 'Value Three'}
# Now you can call the function with kwargs like this:

my_func(**kwargs)

Here’s another approach:

def my_func(arg1, arg2, arg3):
    ... so something ...

kwargs = {'arg1': 'Value One', 'arg2': 'Value Two', 'arg3': 'Value Three'}
# Now you can call the function with kwargs like this:

my_func(**kwargs)

回答 7

我认为在**kwargs默认值上使用Python 的正确方法是使用dictionary方法setdefault,如下所示:

class ExampleClass:
    def __init__(self, **kwargs):
        kwargs.setdefault('val', value1)
        kwargs.setdefault('val2', value2)

这样,如果用户在关键字中传递了’val’或’val2′ args,则将使用它们。否则,将使用已设置的默认值。

I think the proper way to use **kwargs in Python when it comes to default values is to use the dictionary method setdefault, as given below:

class ExampleClass:
    def __init__(self, **kwargs):
        kwargs.setdefault('val', value1)
        kwargs.setdefault('val2', value2)

In this way, if a user passes ‘val’ or ‘val2’ in the keyword args, they will be used; otherwise, the default values that have been set will be used.


回答 8

你可以做这样的事情

class ExampleClass:
    def __init__(self, **kwargs):
        arguments = {'val':1, 'val2':2}
        arguments.update(kwargs)
        self.val = arguments['val']
        self.val2 = arguments['val2']

You could do something like this

class ExampleClass:
    def __init__(self, **kwargs):
        arguments = {'val':1, 'val2':2}
        arguments.update(kwargs)
        self.val = arguments['val']
        self.val2 = arguments['val2']

回答 9

跟进@srhegde建议使用setattr的建议:

class ExampleClass(object):
    __acceptable_keys_list = ['foo', 'bar']

    def __init__(self, **kwargs):
        [self.__setattr__(key, kwargs.get(key)) for key in self.__acceptable_keys_list]

当期望该类具有acceptable列表中的所有项目时,此变体很有用。

Following up on @srhegde suggestion of using setattr:

class ExampleClass(object):
    __acceptable_keys_list = ['foo', 'bar']

    def __init__(self, **kwargs):
        [self.__setattr__(key, kwargs.get(key)) for key in self.__acceptable_keys_list]

This variant is useful when the class is expected to have all of the items in our acceptable list.


回答 10

如果要将其与* args结合使用,则必须在定义末尾保留* args和** kwargs。

所以:

def method(foo, bar=None, *args, **kwargs):
    do_something_with(foo, bar)
    some_other_function(*args, **kwargs)

If you want to combine this with *args you have to keep *args and **kwargs at the end of the definition.

So:

def method(foo, bar=None, *args, **kwargs):
    do_something_with(foo, bar)
    some_other_function(*args, **kwargs)

回答 11

@AbhinavGupta和@Steef建议使用update(),这对处理大型参数列表非常有帮助:

args.update(kwargs)

如果我们要检查用户是否未传递任何虚假/不受支持的参数,该怎么办?@VinaySajip指出,pop()可以用来迭代处理参数列表。然后,任何剩余的参数都是虚假的。真好

这是执行此操作的另一种可能方法,保留了using的简单语法update()

# kwargs = dictionary of user-supplied arguments
# args = dictionary containing default arguments

# Check that user hasn't given spurious arguments
unknown_args = user_args.keys() - default_args.keys()
if unknown_args:
    raise TypeError('Unknown arguments: {}'.format(unknown_args))

# Update args to contain user-supplied arguments
args.update(kwargs)

unknown_args是一个set包含默认值中不出现的参数名称。

@AbhinavGupta and @Steef suggested using update(), which I found very helpful for processing large argument lists:

args.update(kwargs)

What if we want to check that the user hasn’t passed any spurious/unsupported arguments? @VinaySajip pointed out that pop() can be used to iteratively process the list of arguments. Then, any leftover arguments are spurious. Nice.

Here’s another possible way to do this, which keeps the simple syntax of using update():

# kwargs = dictionary of user-supplied arguments
# args = dictionary containing default arguments

# Check that user hasn't given spurious arguments
unknown_args = user_args.keys() - default_args.keys()
if unknown_args:
    raise TypeError('Unknown arguments: {}'.format(unknown_args))

# Update args to contain user-supplied arguments
args.update(kwargs)

unknown_args is a set containing the names of arguments that don’t occur in the defaults.


回答 12

处理未知或多个参数的另一种简单解决方案可以是:

class ExampleClass(object):

    def __init__(self, x, y, **kwargs):
      self.x = x
      self.y = y
      self.attributes = kwargs

    def SomeFunction(self):
      if 'something' in self.attributes:
        dosomething()

Another simple solution for processing unknown or multiple arguments can be:

class ExampleClass(object):

    def __init__(self, x, y, **kwargs):
      self.x = x
      self.y = y
      self.attributes = kwargs

    def SomeFunction(self):
      if 'something' in self.attributes:
        dosomething()

回答 13

** kwargs可以自由添加任意数量的关键字参数。可能会有一个密钥列表,他可以为其设置默认值。但是,不必为无限数量的键设置默认值。最后,将键作为实例属性可能很重要。因此,我将执行以下操作:

class Person(object):
listed_keys = ['name', 'age']

def __init__(self, **kwargs):
    _dict = {}
    # Set default values for listed keys
    for item in self.listed_keys: 
        _dict[item] = 'default'
    # Update the dictionary with all kwargs
    _dict.update(kwargs)

    # Have the keys of kwargs as instance attributes
    self.__dict__.update(_dict)

**kwargs gives the freedom to add any number of keyword arguments. One may have a list of keys for which he can set default values. But setting default values for an indefinite number of keys seems unnecessary. Finally, it may be important to have the keys as instance attributes. So, I would do this as follows:

class Person(object):
listed_keys = ['name', 'age']

def __init__(self, **kwargs):
    _dict = {}
    # Set default values for listed keys
    for item in self.listed_keys: 
        _dict[item] = 'default'
    # Update the dictionary with all kwargs
    _dict.update(kwargs)

    # Have the keys of kwargs as instance attributes
    self.__dict__.update(_dict)

** wargs的目的和用途是什么?

问题:** wargs的目的和用途是什么?

**kwargsPython 的用途是什么?

我知道您可以objects.filter在表上进行传递**kwargs参数。  

我还可以指定时间增量timedelta(hours = time1)吗?

它是如何工作的?它被归类为“拆包”吗?喜欢a,b=1,2吗?

What are the uses for **kwargs in Python?

I know you can do an objects.filter on a table and pass in a **kwargs argument.  

Can I also do this for specifying time deltas i.e. timedelta(hours = time1)?

How exactly does it work? Is it classes as ‘unpacking’? Like a,b=1,2?


回答 0

您可以**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指南,包含了如何工作的,有一些很好的例子沿着一个很好的解释。

<-更新->

对于使用Python 3的用户,请使用items()代替iteritems()

You can use **kwargs to let your functions take an arbitrary number of keyword arguments (“kwargs” means “keyword arguments”):

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

You can also use the **kwargs syntax when calling functions by constructing a dictionary of keyword arguments and passing it to your function:

>>> kwargs = {'first_name': 'Bobby', 'last_name': 'Smith'}
>>> print_keyword_args(**kwargs)
first_name = Bobby
last_name = Smith

The Python Tutorial contains a good explanation of how it works, along with some nice examples.

<–Update–>

For people using Python 3, instead of iteritems(), use items()


回答 1

开箱字典

** 打开字典包装。

这个

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)

Unpacking dictionaries

** unpacks dictionaries.

This

func(a=1, b=2, c=3)

is the same as

args = {'a': 1, 'b': 2, 'c':3}
func(**args)

It’s useful if you have to construct parameters:

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)

Packing parameters of a function

def setstyle(**styles):
    for key, value in styles.iteritems():      # styles is a regular dictionary
        setattr(someobject, key, value)

This lets you use the function like this:

setstyle(color="red", bold=False)

回答 2

kwargs只是添加到参数的字典。

字典可以包含键,值对。那就是怪兽。好的,这就是方法。

目的不是那么简单。

例如(非常假设),您有一个仅调用其他例程来完成工作的接口:

def myDo(what, where, why):
   if what == 'swim':
      doSwim(where, why)
   elif what == 'walk':
      doWalk(where, why)
   ...

现在,您将获得一个新方法“ drive”:

elif what == 'drive':
   doDrive(where, why, vehicle)

但是请稍等,这里有一个新的参数“ 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有帮助。

kwargs is just a dictionary that is added to the parameters.

A dictionary can contain key, value pairs. And that are the kwargs. Ok, this is how.

The what for is not so simple.

For example (very hypothetical) you have an interface that just calls other routines to do the job:

def myDo(what, where, why):
   if what == 'swim':
      doSwim(where, why)
   elif what == 'walk':
      doWalk(where, why)
   ...

Now you get a new method “drive”:

elif what == 'drive':
   doDrive(where, why, vehicle)

But wait a minute, there is a new parameter “vehicle” — you did not know it before. Now you must add it to the signature of the myDo-function.

Here you can throw kwargs into play — you just add kwargs to the signature:

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

This way you don’t need to change the signature of your interface function every time some of your called routines might change.

This is just one nice example you could find kwargs helpful.


回答 3

基于好的样本有时比冗长的论述更好,我将使用所有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})

On the basis that a good sample is sometimes better than a long discourse I will write two functions using all python variable argument passing facilities (both positional and named arguments). You should easily be able to see what it does by yourself:

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)

And here is the output:

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})

回答 4

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没有智能,它只是将传入的args插值到参数(按从左到右的顺序),同时**kwargs通过将适当的值放在所需的位置@来智能地运行

Motif: *args and **kwargs serves as a placeholder for the arguments that need to be passed to a function call

using *args and **kwargs to call a function

def args_kwargs_test(arg1, arg2, arg3):
    print "arg1:", arg1
    print "arg2:", arg2
    print "arg3:", arg3

Now we’ll use *args to call the above defined function

#args can either be a "list" or "tuple"
>>> args = ("two", 3, 5)  
>>> args_kwargs_test(*args)

result:

arg1: two
arg2: 3
arg3: 5


Now, using **kwargs to call the same function

#keyword argument "kwargs" has to be a dictionary
>>> kwargs = {"arg3":3, "arg2":'two', "arg1":5}
>>> args_kwargs_test(**kwargs)

result:

arg1: 5
arg2: two
arg3: 3

Bottomline : *args has no intelligence, it simply interpolates the passed args to the parameters(in left-to-right order) while **kwargs behaves intelligently by placing the appropriate value @ the required place


回答 5

  • kwargs**kwargs只是变量名。你可以很好地拥有**anyVariableName
  • kwargs代表“关键字参数”。但是我觉得最好将它们称为“命名参数”,因为它们只是随名称一起传递的参数(我对“关键字参数”一词中的“关键字”一词没有任何意义。我猜“关键字”通常是指编程语言保留的单词,因此程序员不要将其用于变量名。因此,我们给名称 param1param2两个传递给函数的参数值如下:func(param1="val1",param2="val2"),而不是仅传递值:func(val1,val2)。因此,我认为应将它们适当地称为“命名参数的任意数量”,因为我们可以指定任意数量的这些参数(即,funcfunc(**kwargs)

可以这么说,让我先解释“命名参数”,然后再解释“任意数量的命名参数” kwargs

命名参数

  • 命名的args应该跟随位置args
  • args的顺序并不重要
  • 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

  • 功能参数顺序:
    1. 位置参数
    2. 捕获任意数量参数的形式参数(带*前缀)
    3. 命名形式参数
    4. 形式参数,用于捕获任意数量的命名参数(带**前缀)
  • 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

为自定义参数传递元组和dict变量

最后,请允许我注意我们可以通过

  • 作为元组变量的“捕获任意数量参数的形式参数”
  • “形式参数捕获任意数量的命名参数”作为dict变量

因此,可以进行以下相同的调用:

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参数:

function2("arg1",
      *tupleCustomArgs,   
      param3="arg3",
      param2="arg2", 
      dictCustomNamedArgs   #omitting **
      )

dictCustomNamedArgs
         ^
SyntaxError: non-keyword arg after keyword arg
  • kwargs in **kwargs is just variable name. You can very well have **anyVariableName
  • kwargs stands for “keyword arguments”. But I feel they should better be called as “named arguments”, as these are simply arguments passed along with names (I dont find any significance to the word “keyword” in the term “keyword arguments”. I guess “keyword” usually means words reserved by programming language and hence not to be used by the programmer for variable names. No such thing is happening here in case of kwargs.). So we give names param1 and param2 to two parameter values passed to the function as follows: func(param1="val1",param2="val2"), instead of passing only values: func(val1,val2). Thus, I feel they should be appropriately called “arbitrary number of named arguments” as we can specify any number of these parameters (that is, arguments) if func has signature func(**kwargs)

So being said that let me explain “named arguments” first and then “arbitrary number of named arguments” kwargs.

Named arguments

  • named args should follow positional args
  • order of named args is not important
  • Example

    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'
    

Arbitrary number of named arguments kwargs

  • Sequence of function parameters:
    1. positional parameters
    2. formal parameter capturing arbitrary number of arguments (prefixed with *)
    3. named formal parameters
    4. formal parameter capturing arbitrary number of named parameters (prefixed with **)
  • Example

    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
    

Passing tuple and dict variables for custom args

To finish it up, let me also note that we can pass

  • “formal parameter capturing arbitrary number of arguments” as tuple variable and
  • “formal parameter capturing arbitrary number of named parameters” as dict variable

Thus the same above call can be made as follows:

tupleCustomArgs = ("custom param1", "custom param2", "custom param3")
dictCustomNamedArgs = {"customNamedParam1":"val1", "customNamedParam2":"val2"}

function2("arg1",
      *tupleCustomArgs,    #note *
      param3="arg3",
      param2="arg2", 
      **dictCustomNamedArgs     #note **
      )

Finally note * and ** in function calls above. If we omit them, we may get ill results.

Omitting * in tuple args:

function2("arg1",
      tupleCustomArgs,   #omitting *
      param3="arg3",
      param2="arg2", 
      **dictCustomNamedArgs
      )

prints

param1: arg1
param2: arg2
param3: arg3
custom tuple params ----------
('custom param1', 'custom param2', 'custom param3'),
custom named params ----------
customNamedParam2:val2
customNamedParam1:val1

Above tuple ('custom param1', 'custom param2', 'custom param3') is printed as is.

Omitting dict args:

function2("arg1",
      *tupleCustomArgs,   
      param3="arg3",
      param2="arg2", 
      dictCustomNamedArgs   #omitting **
      )

gives

dictCustomNamedArgs
         ^
SyntaxError: non-keyword arg after keyword arg

回答 6

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

As an addition, you can also mix different ways of usage when calling kwargs functions:

def test(**kwargs):
    print kwargs['a']
    print kwargs['b']
    print kwargs['c']


args = { 'b': 2, 'c': 3}

test( a=1, **args )

gives this output:

1
2
3

Note that **kwargs has to be the last argument


回答 7

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

kwargs are a syntactic sugar to pass name arguments as dictionaries(for func), or dictionaries as named arguments(to func)


回答 8

这是一个用于解释用法的简单函数:

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

Here’s a simple function that serves to explain the usage:

def print_wrap(arg1, *args, **kwargs):
    print(arg1)
    print(args)
    print(kwargs)
    print(arg1, *args, **kwargs)

Any arguments that are not specified in the function definition will be put in the args list, or the kwargs list, depending on whether they are keyword arguments or not:

>>> print_wrap('one', 'two', 'three', end='blah', sep='--')
one
('two', 'three')
{'end': 'blah', 'sep': '--'}
one--two--threeblah

If you add a keyword argument that never gets passed to a function, an error will be raised:

>>> print_wrap('blah', dead_arg='anything')
TypeError: 'dead_arg' is an invalid keyword argument for this function

回答 9

这是一个示例,希望对您有所帮助:

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

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

Here is an example that I hope is helpful:

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

When you run the program, you get:

$ 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

The key take away here is that the variable number of named arguments in the call translate into a dictionary in the function.


回答 10

这是了解python拆包的简单示例,

>>> def f(*args, **kwargs):
...    print 'args', args, 'kwargs', kwargs

eg1:

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

This is the simple example to understand about python unpacking,

>>> def f(*args, **kwargs):
...    print 'args', args, 'kwargs', kwargs

eg1:

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

回答 11

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

只是另一种思考方式。

In Java, you use constructors to overload classes and allow for multiple input parameters. In python, you can use kwargs to provide similar behavior.

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

python example:

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.

just another way to think about it.


回答 12

关键字参数通常在Python中简化为kwargs。在计算机编程中

关键字参数指的是一种计算机语言对函数调用的支持,该函数清楚地说明了函数调用中每个参数的名称。

参数名称** kwargs之前的两个星号的用法是,当一个人不知道将多少个关键字参数传递给该函数时。在这种情况下,它称为任意/通配符关键字参数。

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

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

请注意,该函数带有一个sender参数以及通配符关键字参数(** kwargs);所有信号处理程序都必须采用这些参数。所有信号都发送关键字参数,并且可以随时更改这些关键字参数。在request_finished的情况下,它被记录为不发送任何参数,这意味着我们可能很想将信号处理编写为my_callback(sender)。

这将是错误的-实际上,如果您这样做,Django将抛出错误。这是因为在任何时候都可以将参数添加到信号中,并且您的接收器必须能够处理这些新参数。

请注意,它不必称为kwargs,但必须具有**(名称kwargs是一个约定)。

Keyword Arguments are often shortened to kwargs in Python. In computer programming,

keyword arguments refer to a computer language’s support for function calls that clearly state the name of each parameter within the function call.

The usage of the two asterisk before the parameter name, **kwargs, is when one doesn’t know how many keyword arguments will be passed into the function. When that’s the case, it’s called Arbitrary / Wildcard Keyword Arguments.

One example of this is Django’s receiver functions.

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

Notice that the function takes a sender argument, along with wildcard keyword arguments (**kwargs); all signal handlers must take these arguments. All signals send keyword arguments, and may change those keyword arguments at any time. In the case of request_finished, it’s documented as sending no arguments, which means we might be tempted to write our signal handling as my_callback(sender).

This would be wrong – in fact, Django will throw an error if you do so. That’s because at any point arguments could get added to the signal and your receiver must be able to handle those new arguments.

Note that it doesn’t have to be called kwargs, but it needs to have ** (the name kwargs is a convention).