<small id='JAdrV'></small><noframes id='JAdrV'>

    <tfoot id='JAdrV'></tfoot>

        • <bdo id='JAdrV'></bdo><ul id='JAdrV'></ul>
        <i id='JAdrV'><tr id='JAdrV'><dt id='JAdrV'><q id='JAdrV'><span id='JAdrV'><b id='JAdrV'><form id='JAdrV'><ins id='JAdrV'></ins><ul id='JAdrV'></ul><sub id='JAdrV'></sub></form><legend id='JAdrV'></legend><bdo id='JAdrV'><pre id='JAdrV'><center id='JAdrV'></center></pre></bdo></b><th id='JAdrV'></th></span></q></dt></tr></i><div id='JAdrV'><tfoot id='JAdrV'></tfoot><dl id='JAdrV'><fieldset id='JAdrV'></fieldset></dl></div>

      1. <legend id='JAdrV'><style id='JAdrV'><dir id='JAdrV'><q id='JAdrV'></q></dir></style></legend>
      2. 为什么属性名称不能是 Python 关键字?

        时间:2023-09-13

              <tbody id='BqrnT'></tbody>
          • <tfoot id='BqrnT'></tfoot>
            <i id='BqrnT'><tr id='BqrnT'><dt id='BqrnT'><q id='BqrnT'><span id='BqrnT'><b id='BqrnT'><form id='BqrnT'><ins id='BqrnT'></ins><ul id='BqrnT'></ul><sub id='BqrnT'></sub></form><legend id='BqrnT'></legend><bdo id='BqrnT'><pre id='BqrnT'><center id='BqrnT'></center></pre></bdo></b><th id='BqrnT'></th></span></q></dt></tr></i><div id='BqrnT'><tfoot id='BqrnT'></tfoot><dl id='BqrnT'><fieldset id='BqrnT'></fieldset></dl></div>

          • <small id='BqrnT'></small><noframes id='BqrnT'>

              <bdo id='BqrnT'></bdo><ul id='BqrnT'></ul>
              <legend id='BqrnT'><style id='BqrnT'><dir id='BqrnT'><q id='BqrnT'></q></dir></style></legend>
                1. 本文介绍了为什么属性名称不能是 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
                  

                  我的问题有两个:

                  1. 禁止使用 Python 关键字属性名称(如 o.if = 123)是否有根本原因?
                  2. 是否/在哪里记录了上述对属性名称的限制?

                  在我的一个程序中执行 o.class = ... 是有意义的,但我对无法执行此操作有点失望(o.class_code> 会起作用,但看起来并不简单).

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

                  PS:问题显然是ifclass是Python关键字.问题是 why 禁止使用关键字作为属性名称(我在表达式 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.

                  推荐答案

                  因为当关键字总是关键字而不是上下文时解析器更简单(例如,if 在语句级别时是关键字,但是只是在表达式中的标识符 - 对于 if 它会是双重困难,因为 X if C else Y,并且 for 用于列表推导式和生成器表达式).

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

                  所以代码甚至没有到达属性访问的地步,它只是被解析器拒绝,就像不正确的缩进一样(这就是为什么它是 SyntaxError,而不是 AttributeError 什么的).它不区分您是使用 if 作为属性名、变量名、函数名还是类型名.它永远不可能是标识符,因为解析器总是为其分配关键字"标签,并使其成为与标识符不同的标记.

                  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__dict__ 来制作具有保留名称的属性,并不意味着您应该.不要强迫自己/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 关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何使用 Python 的 zipfile 模块对 ZIP 文件中的文件 下一篇:hasattr() 与 try-except 块处理不存在的属性

                  相关文章

                  最新文章

                  <small id='IL2FK'></small><noframes id='IL2FK'>

                  <tfoot id='IL2FK'></tfoot>
                      <bdo id='IL2FK'></bdo><ul id='IL2FK'></ul>

                  1. <i id='IL2FK'><tr id='IL2FK'><dt id='IL2FK'><q id='IL2FK'><span id='IL2FK'><b id='IL2FK'><form id='IL2FK'><ins id='IL2FK'></ins><ul id='IL2FK'></ul><sub id='IL2FK'></sub></form><legend id='IL2FK'></legend><bdo id='IL2FK'><pre id='IL2FK'><center id='IL2FK'></center></pre></bdo></b><th id='IL2FK'></th></span></q></dt></tr></i><div id='IL2FK'><tfoot id='IL2FK'></tfoot><dl id='IL2FK'><fieldset id='IL2FK'></fieldset></dl></div>

                    1. <legend id='IL2FK'><style id='IL2FK'><dir id='IL2FK'><q id='IL2FK'></q></dir></style></legend>