"as" keyword woes
warren at delsci.com
Wed Dec 3 22:38:23 CET 2008
A bottom line / pragmatic question... hopefully not a FAQ.
Why was it necessary to make "as" a reserved keyword?
And more to the point, why was it necessary to prevent developers from
being able to refer to attributes named "as"?
For example, this code breaks as of 2.6 / 3.0:
obj = C()
obj.bs = 2 # valid
obj.as = 1 # syntax error
obj.cs = 3 # valid
Just to be clear: I understand why it breaks, since "as" is now a
keyword, I also know that one can use workarounds such as:
obj.__dict__['as'] = 1
to maintain compatibility.
What I want to understand is why this parser change was necessary in
order to enable new 2.6/3.0 features. Was this change potentially
Why can't the parser distinguish between a standalone " as " keyword and
".as" used as an object/attribute reference?
Couldn't we have continued along just fine using a smarter parser
without elevating "as" to reserved status (and thus potentially breaking
a 10+ years of existing code)?
Thank you for enlighting me!
(Unfortunately, our project may now have to maintain a branch at 2.5.x
in order to preserve compatibility with existing third-party scripts &
infrastructure which routinely rely upon "as" as an object method.
More information about the Python-list