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:


>>> class C:
...     def __init__(self, flavor):
...         self.flavor = flavor
>>> c=C('garlicky')
>>> getattr(c, 'flavor')
>>> getattr(c, 'aroma', 'bland')
>>> getattr(c, 'aroma')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: C instance has no attribute 'aroma'