功能:**kwargs 允许不正确命名的变量 [英] feature: **kwargs allowing improperly named variables

查看:34
本文介绍了功能:**kwargs 允许不正确命名的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是无效的python:

The following is invalid python:

def myInvalidFun(kw arg zero=6):
    pass

以下是有效的python:

The following is valid python:

def myValidFun(**kwargs):
    if kwargs.has_key("kw arg zero"):
        pass

然而,调用 myValidFun 很棘手.例如,接下来的几种方法不起作用:

To call myValidFun, however, is tricky. For instance, the next few approaches do not work:

myValidFun(kw arg zero=6)      # SyntaxError: invalid syntax
myValidFun("kw arg zero"=6)    # SyntaxError: keyword can't be an expression

kwargs = dict("kw arg zero"=6) # SyntaxError: keyword can't be an expression 
myValidFun(**kwargs)

(也许与最后两个相同的错误暗示了幕后发生了什么?)然而,这确实有效:

(Perhaps the identical errors to the last two hint at what happens under the hood?) This, however, DOES work:

kwargs = {"kw arg zero": 6}
myValidFun(**kwargs)

根据用于创建字典的 {:} 语法,是否有原因,特别是 myValidFun("kw arg zero"=6) 无效?

Is there a reason why, in particular, myValidFun("kw arg zero"=6) is not valid, in light of the {:} syntax for creating dictionaries?

(更多背景:我有一个很像字典的类,只有大量的验证,还有一个 __init__ 使用字典的条目构建一个容器,但不是一个字典...它实际上是一个 XML 元素树,它在某些方面类似于列表,而在其他方面类似于字典.__init__ 方法必须采用诸如my first element"和my_first_element"之类的键和考虑它们不同的事情.类和 __init__ 与 **kwargs 一起工作得很好,但是初始化我的类是我的示例形式的多行代码,它确实有效,而且看起来可能更简单.)

(More background: I have a class which is much like a dictionary, only with significant amounts of validation, and an __init__ which builds a container using the entries of the dictionary, but is not a dictionary... it is actually an XML ElementTree, which is in some ways list-like and in others dict-like. The __init__ method must take keys like "my first element" and "my_first_element" and consider them different things. The class and __init__ work fine with **kwargs, but initializing my class is a multi-liner in the form of my example which does work, and seems like it could be simpler.)

我理解标识符的概念,我的无效代码是为了说明这一点.我想我的问题应该改写为:

edit: I understand the concept of identifiers, and my invalid code is there to make a point. I guess my question should be rephrased as:

为什么以下内容有效?:

Why is the following valid?:

myValidFun(**{"invalid identifier":6})

推荐答案

python 函数的关键字必须是有效的标识符.这是因为在另一方面,它们需要被解压成标识符(变量):

Keywords to python functions must be valid identifiers. This is because on the other side, they need to be unpacked into identifiers (variables):

def foo(arg=3):
    print arg

您拥有的大多数东西都不是有效标识符:

most of the things you have are not valid identifiers:

kw arg zero  #Not valid identifier -- Can't have spaces
"kw arg zero" #Not valid identifier -- It's parsed as a string (expression)

在做

dict("kw arg zero" = 6)

与解析器没有什么不同

is no different to the parser than

myValidFunc("kw arg zero" = 6)

<小时>

现在正如您所指出的,您可以传递视图映射打包/解包(**kwargs).但是,它只能通过字典访问.


now as you've pointed out, You can pass things view mapping packing/unpacking (**kwargs). However, it can only be accessed through a dictionary.

这篇关于功能:**kwargs 允许不正确命名的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆