Note about getattr and '.'

Jonathan Ballet multani at free.fr
Tue Nov 21 23:03:57 CET 2006


Le 21 Nov 2006 13:21:52 -0800,
szport at gmail.com a écrit :

> There is an interesting skewness in python:
> 
> class A(object): pass
> 
> >>> a=A()
> >>> setattr(a, '$foo', 17)
> >>> getattr(a, '$foo')
> 17
> 
> But I can't write 
> >>> a.'$foo'
> 

Well, you can even do :

>>> class A(object): pass
...
>>> a = A()
>>> setattr(a, 'some.attr', 15)
>>> a.some.attr
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'A' object has no attribute 'some'
>>> getattr(a, 'some.attr')
15
>>>

Which is a lot funnier IMHO :)

 - Jon



More information about the Python-list mailing list