[Python-ideas] relaxing keyword usage restrictions

Barry Warsaw barry at python.org
Wed Sep 7 00:33:46 CEST 2011


On Sep 07, 2011, at 07:54 AM, Nick Coghlan wrote:

>Ultimately, it's the "keep the rules simple" argument that holds the
>most weight with me on this topic, so the twin rules of "keywords can
>only be used as keywords" and "attribute names must be legal
>identifiers" end up being my preferred approach. The occasional
>inconvenience of a deliberate misspelling or the addition of a
>trailing underscore to convert a keyword into a legal identifier seems
>like a worthwhile trade-off for the corresponding simplification of
>the language syntax and semantics.

Still not disagreeing with you, but in some sense, you *can* use keywords as
attributes today:

Python 3.2.2 (default, Sep  5 2011, 21:17:14) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo: pass
... 
>>> f = Foo()
>>> setattr(f, 'class', 'evil')
>>> f.class
  File "<stdin>", line 1
    f.class
          ^
SyntaxError: invalid syntax
>>> getattr(f, 'class')
'evil'
>>> 
>>> dir(f)
['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'class']

You just have to use "weird" syntax to set and get the values of those
attributes. ;)  So much for consistency.

-Barry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110906/23270822/attachment.pgp>


More information about the Python-ideas mailing list