为什么在Python中无法将类属性命名为保留字? [英] Why can't class attributes be named as reserved words in python?

查看:142
本文介绍了为什么在Python中无法将类属性命名为保留字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎保留字不能在python中用作属性:

It seems reserved words can not be used as attributes in python:

$ python
Python 3.6.2 |Continuum Analytics, Inc.| (default, Jul 20 2017, 13:51:32) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
>>>     global = 3
  File "<stdin>", line 2
    global = 3
           ^
SyntaxError: invalid syntax

这似乎是明智的,因为它是模棱两可的:我在这里使用global关键字吗?很难说.

This seems sensible, since it is ambiguous: am I using the global keyword here? Difficult to say.

但这不是明智的恕我直言:

But this is not sensible imho:

>>> class A: pass
>>> a = A()
>>> a.global = 3
  File "<stdin>", line 1
    a.global = 3
           ^
SyntaxError: invalid syntax
>>> a.def = 4
  File "<stdin>", line 1
    a.def = 4
        ^
SyntaxError: invalid syntax
>>> a.super = 5
>>> a.abs = 3
>>> a.set = 5
>>> a.False = 5
  File "<stdin>", line 1
    a.False = 5
          ^
SyntaxError: invalid syntax
>>> a.break = 5
  File "<stdin>", line 1
    a.break = 5
          ^
SyntaxError: invalid syntax

为什么有此限制?我不是在孤立地使用保留字,而是将其作为类属性:根本没有歧义. python为什么会在乎呢?

Why this limitation? I am not using the reserved words in isolation, but as a class attribute: there is not ambiguity at all. Why would python care about that?

推荐答案

这不值得.

当然可以.修改令牌解析器和解析器,以便令牌解析器了解解析上下文,并在解析器期望属性访问时发出NAME令牌而不是关键字令牌,或者只是在DOT之后始终发出NAME令牌而不是关键字.但是那会给你带来什么呢?

Sure, you could allow it. Hack up the tokenizer and the parser so the tokenizer is aware of the parse context and emits NAME tokens instead of keyword tokens when the parser is expecting an attribute access, or just have it always emit NAME tokens instead of keywords after a DOT. But what would that get you?

您将使解析器和令牌生成器更加复杂,从而更易于发生错误.对于人类读者来说,这会使事情变得更难阅读.您将限制将来使用语法的可能性.

You'd make the parser and tokenizer more complicated, and thus more bug-prone. You'd make things harder to read for a human reader. You'd restrict future syntax possibilities. You'd cause confusion when

Foo.for = 3

解析和

class Foo:
    for = 3

引发SyntaxError.您会降低Python的一致性,学习难度和理解程度.

throws a SyntaxError. You'd make Python less consistent, harder to learn, and harder to understand.

所有这些,您将获得...编写x.for = 3的能力.为此,我能说的最好的一点是,它可以防止在添加fibble关键字时破坏x.fibble = 3之类的东西,但是即使那样,fibble的所有其他用法仍然会破坏.不值得.如果要使用疯狂的属性名称,请使用setattrgetattr.

And for all that, you'd gain... the ability to write x.for = 3. The best I can say for this is that it'd prevent something like x.fibble = 3 from breaking upon addition of a fibble keyword, but even then, all other uses of fibble would still break. Not worth it. If you want to use crazy attribute names, you have setattr and getattr.

Python尽最大努力使语法简单.它的解析器是LL(1),并且LL(1)解析器的限制被认为是有益的.特别是因为它们可以防止过度使用疯狂的语法规则:

Python does its best to make the syntax simple. Its parser is LL(1), and the restrictions of an LL(1) parser are considered beneficial specifically because they prevent going overboard with crazy grammar rules:

简单胜于复杂.这个想法扩展到了解析器.将Python的语法限制为LL(1)解析器是一种祝福,而不是诅咒.它使我们陷入束手无策的境地,从而阻止我们过度学习并以时髦的语法规则结尾,就像其他一些不愿透露名称的动态语言(例如Perl)一样.

Simple is better than complex. This idea extends to the parser. Restricting Python's grammar to an LL(1) parser is a blessing, not a curse. It puts us in handcuffs that prevent us from going overboard and ending up with funky grammar rules like some other dynamic languages that will go unnamed, such as Perl.

x.for = 3之类的东西与该设计理念不符.

Something like x.for = 3 is not in keeping with that design philosophy.

这篇关于为什么在Python中无法将类属性命名为保留字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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