SomeClassInstance.[attributename]

Hrvoje Niksic hniksic at arsdigita.com
Mon Nov 6 10:52:24 EST 2000


tattoom at my-deja.com writes:

> Hello,Please excuse my ignorance...Python does provide a built-in function to
> check for the existence of an object's attribute:  hasattr(object, name)What
> puzzles me to no end, is the fact one straight (IMHO) corollary to the above
> functionality doesn't seem to exist (at least I can't find it :-).Namely, the
> possibility to access an instance's attribute in a 'parameterized' (is that a
> correct word?) way..e.g. Assume that Foo is an instance of some
> class:theAttribute = 'fubar'if hasattr(Foo, theAttribute):  fubar =
> Foo.[theAttribute]# Where Foo.[theAttribute] would yield Foo.fubarYou get my
> point. Is there a way in Python (as elegant and straightforward as this one)
> to achieve the above?

getattr(foo, attribute)

(Corollary to hasattr, right?)



More information about the Python-list mailing list