[Python-ideas] relaxing keyword usage restrictions

Matt Joiner anacrolix at gmail.com
Wed Sep 7 01:44:09 CEST 2011

I was -1 until you brought up consistency. I'm going to wave the flag
of "least surprising behaviour" and change my vote to +0. Yes it makes
it harder to parse, but it's easier for humans :P

On Wed, Sep 7, 2011 at 8:33 AM, Barry Warsaw <barry at python.org> wrote:
> 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
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas

More information about the Python-ideas mailing list