Simple or Impossible in Python... dynamically dereferencing a class attribute

Michael Cornelius michael at ninthorder.com
Wed Dec 24 18:06:28 CET 2003


Bill Sneddon wrote:
> Given the following simple code.  I was trying find a way to
>  dynamically de-reference a class attribute.

Have a look at getattr/setattr.

 >>> class bogus: pass
 >>> b = bogus()
 >>> myAttr = 'a'
 >>> setattr(b, myAttr, 'foo')
 >>> b.a
'foo'
 >>> getattr(b, myAttr)
'foo'







More information about the Python-list mailing list