[Python-ideas] relaxing keyword usage restrictions
ncoghlan at gmail.com
Wed Sep 7 02:21:59 CEST 2011
On Wed, Sep 7, 2011 at 8:33 AM, Barry Warsaw <barry at python.org> wrote:
> Still not disagreeing with you, but in some sense, you *can* use keywords as
> attributes today:
You can actually use arbitrary strings, such as "1234" and "not a
legal identifier" that way, so I see that behaviour as still being
consistent. Some implementations, including CPython, even let you use
arbitrary non-string objects via the attribute dict interface, but
whether or not that works has been explicitly deemed an implementation
detail by Guido.
If you treat a namespace like a string-keyed dictionary, you can use
any string, even those that aren't valid identifiers. Treat it like an
actual namespace, though, and the identifier restrictions come into
play (including those disallowing the use of keywords).
While it is indeed another rule, it's still a fairly simple and
consistent one, since the identifier rules are there to allow
consistent parsing without dedicated delimiters.
That does give me an idea, though. Rather than allowing
keywords-as-attributes, it may make more sense to allow string
literals to stand in for identifiers without obeying the normal rules,
permitting things like:
normal = 1
'class' = 'This is probably a terrible idea'
'1234' = 'as is this'
'or does it' = 'have some merit?'
'This is probably a terrible idea'
'as is this'
>>> Foo.'or does it'
'have some merit?'
I'm still -1 myself, but such a delimited approach at least has the
virtue of addressing the *entire* scope of the identifier syntax
limitation rather than singling out keywords for special treatment.
Legitimate use cases for such a feature would include at least those
currently handled by the 'rename' flag on the namedtuple constructor
(the latter would still be needed to handle duplicate field names, but
illegal identifiers could just be quoted).
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas