setattr using invalid attribute names - bug or feature?

Meno dontaskme at doityourself.com
Mon Jun 14 20:02:06 CEST 2004


gerson.kurz at t-online.de (Gerson Kurz) wrote in message news:<40cdb3d5.2146906 at news.t-online.de>...
> I stumbled across this (while using my homebrewn enum class):
> 
>     class test:
>         pass
> 
>     instance = test()
>     setattr(instance, "THIS :*2+~# IS OBVIOUSLY INVALID", 123)
> 
> I would've expected some kind of error message here when calling
> setattr(); after all, its not a regular attribute? Plus, documentation
> says
> 
> "
>  Set a named attribute on an object; setattr(x, 'y', v) is equivalent
> to
>     ``x.y = v''.
> "
> 
> and you cannot write this:
> 
> instance.THIS :*2+~# IS OBVIOUSLY INVALID = 123

No, but you can write this:

>>> a = getattr(instance, "THIS :*2+~# IS OBVIOUSLY INVALID")
>>> print a
123

Meno.



More information about the Python-list mailing list