[Python-ideas] Bug? Feature? setattr(foo, '3', 4) works!

Nick Coghlan ncoghlan at gmail.com
Fri Dec 19 12:00:17 CET 2014

On 19 December 2014 at 15:15, Chris Angelico <rosuav at gmail.com> wrote:

> On Fri, Dec 19, 2014 at 3:27 PM, Cem Karan <cfkaran2 at gmail.com> wrote:
> > I'd like to suggest that getattr(), setattr(), and hasattr() all be
> modified so that syntactically invalid statements raise SyntaxErrors.
> Is there actually a need to have them check like that? :) After all,
> attributes are just stored in a dictionary. You want
> syntactically-invalid attributes? Sure, no problem.

As far as I'm aware, it's actually an implementation defined behaviour -
implementations are permitted, but not required, to use string-only
dictionaries for namespaces. (Although I can't currently lay my hands on
anything in the language reference that actually *says* that...)

Most implementations will go with whichever is faster in their particular
case (for CPython, that means using the standard permissive dict type, for
Jython, it means only allowing strings, for other implementations, I'm not

CPython is highly unlikely to ever break compatibility over this, but it
*is* a potential cross-implementation portability problem when folks rely
on it.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20141219/09ae4ffd/attachment.html>

More information about the Python-ideas mailing list