[Python-ideas] Bug? Feature? setattr(foo, '3', 4) works!
cfkaran2 at gmail.com
Fri Dec 19 12:17:18 CET 2014
On Dec 19, 2014, at 6:03 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> On 19 December 2014 at 20:57, Cem Karan <cfkaran2 at gmail.com> wrote:
> But, getting back to the main question, is this a bug or a feature? I personally feel like this is a bug, and I'd like to both clarify it in the language spec, and have cpython modified to enforce the syntax, regardless of how you try to mess with an attribute. How does everyone else feel about this?
> Namespaces are just dictionaries. The one thing implementations may do is to place a type restriction on the keys, so they throw TypeError if you try to use something other than str (although *CPython* doesn't do that).
> getattr/setattr/delattr would never throw SyntaxError though - that's only thrown by the compiler.
I see what you're saying, but I feel like having (get|set|has)attr throw TypeError instead of SyntaxError would itself be astonishing in this case. The feature I'm after is that "foo.3" results in exactly the same behavior as "getattr(foo, '3')".
More information about the Python-ideas