[Python-Dev] Non-string keys in type dict

Lennart Regebro regebro at gmail.com
Thu Mar 8 10:10:11 CET 2012


On Thu, Mar 8, 2012 at 08:46, Ethan Furman <ethan at stoneleaf.us> wrote:
> I think it would be sad to lose that functionality.
>
> If we are going to, though, we may as well check the string to make sure
> it's a valid identifier:

That would break even more code. I have encountered many cases of
attributes that aren't valid identifiers, in particular using dots or
dashes. Admittedly this is often in cases where the object has both
attribute access and key access, so you can make foo['bar-frotz']
instead. But when should we then require that it is a valid identifier
and when not?

> --> class A:
> -->   pass
> --> setattr(A, '42', 'hrm')
> --> A.42
>  File "<stdin>", line 1
>    A.42
>       ^
> SyntaxError: invalid syntax
>
> Doesn't seem very useful.

You have to set it with setattr, so you have to get it with getattr. I
don't see the problem.

//Lennart


More information about the Python-Dev mailing list