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 "", line 2

global = 3

^

SyntaxError: invalid syntax

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 "", line 1

a.global = 3

^

SyntaxError: invalid syntax

>>> a.def = 4

File "", line 1

a.def = 4

^

SyntaxError: invalid syntax

>>> a.super = 5

>>> a.abs = 3

>>> a.set = 5

>>> a.False = 5

File "", line 1

a.False = 5

^

SyntaxError: invalid syntax

>>> a.break = 5

File "", line 1

a.break = 5

^

SyntaxError: invalid syntax

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?

解决方案

It's nowhere near worth it.

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

parses and

class Foo:

for = 3

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

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

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.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值