getattr nested attributes

Fredrik Lundh fredrik at pythonware.com
Fri Aug 15 10:10:25 EDT 2008


Gregor Horvath wrote:

> Thank's, but this does not work for this case:
> 
> class A(object):
>     test = "test"
> 
> class B(object):
>     a = [A(),]
> 
> In [70]: reduce(getattr, "a[0].test".split("."), B)
> ---------------------------------------------------------------------------
> <type 'exceptions.AttributeError'>        Traceback (most recent call last)

getattr fetches a named attribute, it doesn't evaluate random code 
snippets.  please read the manual.

</F>




More information about the Python-list mailing list