为什么不能属性名称是Python的关键字? [英] Why can't attribute names be Python keywords?

查看:350
本文介绍了为什么不能属性名称是Python的关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有是对属性访问的语法,在Python(在CPython的执行2.7.2至少)限制:

There is a restriction on the syntax of attribute access, in Python (at least in the CPython 2.7.2 implementation):

>>> class C(object): pass
>>> o = C()
>>> o.x = 123  # Works
>>> o.if = 123
    o.if = 123
       ^
SyntaxError: invalid syntax

我的问题是双重的:

My question is twofold:


  1. 有一个根本原因使用Python关键词属性的名称(如 o.if = 123 )是被禁止的?

  2. 为/,其中是在属性名上述限制记录?

  1. Is there a fundamental reason why using Python keyword attribute names (as in o.if = 123) is forbidden?
  2. Is/where is the above restriction on attribute names documented?

这将是有意义做 o.class = ... ,在我的节目之一,我有点失望没有能够做到这一点( o.class _ 将工作,但它看起来并不那么简单)。

It would make sense to do o.class = …, in one of my programs, and I am a little disappointed not to be able to do it (o.class_ would work, but it does not look as simple).

PS :这个问题显然是如果是Python的关键字。现在的问题是的为什么的使用关键字作为属性名称将被禁止(我看不出在EX pression任何含糊 o.class = 123 ),这是的文件

PS: The problem is obviously that if and class are Python keywords. The question is why using keywords as attribute names would be forbidden (I don't see any ambiguity in the expression o.class = 123), and whether this is documented.

推荐答案

由于分析器是简单的,当关键字总是关键字,而不是上下文(如如果是一个关键字时,在语句级别,而只是一个标识符时前pression里面 - 对如果它会是因为的X 双硬盘如果C否则是列表中的COM prehensions和发电机前pressions)被使用。

Because parser is simpler when keywords are always keywords, and not contextual (e.g. if is a keyword when on the statement level, but just an identifier when inside an expression — for if it'd be double hard because of X if C else Y, and for is used in list comprehensions and generator expressions).

所以,code甚至没有去那里的属性接入点,它只是由解析器拒绝,就像不正确缩进(这就是为什么它是一个的SyntaxError ,而不是 AttributeError的或东西)。它不区分是否使用如果作为属性名,变量名,函数名,或类型名。它不可能是一个标识符,只是因为解析器总是赋予它关键字标签,使得它不同的令牌比标识符。

So the code doesn't even get to the point where there's attribute access, it's simply rejected by the parser, just like incorrect indentation (which is why it's a SyntaxError, and not AttributeError or something). It doesn't differentiate whether you use if as an attribute name, a variable name, a function name, or a type name. It can never be an identifier, simply because parser always assigns it "keyword" label and makes it a different token than identifiers.

这是大多数语言一样,和语言的语法(词法分析器+规范)是该文档。 语言规范提到它明确的。它也不会在Python 3改变。

It's the same in most languages, and language grammar (+ lexer specification) is the documentation for that. Language spec mentions it explicitly. It also doesn't change in Python 3.

此外,仅仅是因为你可以使用 SETATTR __字典__ 来使用预留名称的属性,没有按'代表你的的。不要强迫自己/ API用户使用 GETATTR 而不是自然属性的访问。 GETATTR 应保留需要访问一个变量属性的名称时。

Also, just because you can use setattr or __dict__ to make an attribute with a reserved name, doesn't mean you should. Don't force yourself/API user to use getattr instead of natural attribute access. getattr should be reserved for when access to a variable attribute name is needed.

这篇关于为什么不能属性名称是Python的关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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