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