Next / Previous / Contents / TCC Help System / NM Tech homepage

21.8. getattr(): Retrieve an attribute of a given name

Use this function to retrieve an attribute of an instance I, where the attribute's name is a string s.

getattr(I, s[, default])

If I has no attribute whose name matches s:

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'