Use this function to retrieve an attribute of an instance
, where the
attribute's name is a string I.
s
getattr(I,s[,default])
If has no
attribute whose name matches I:
s
If you supplied the optional value, that value is
returned.
default
If you don't supply a value and there is no such
attribute in default, this function will raise an I exception.
AttributeError
Example:
>>> class C:
... def __init__(self, flavor):
... self.flavor = flavor
...
>>> c=C('garlicky')
>>> getattr(c, 'flavor')
'garlicky'
>>> getattr(c, 'aroma', 'bland')
'bland'
>>> getattr(c, 'aroma')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: C instance has no attribute 'aroma'