First page Back Continue Last page Overview Graphics

Retrieving the Values of Attributes

Trying to access a non-existing attribute raises an AttributeError:

>>> x.energy

Traceback (most recent call last):

File "<stdin>", line 1, in

AttributeError: 'Robot' object has no attribute 'energy'

>>>

This exception can be avoided by using getattr, especially with the third argument as the default value for the attribute::

>>> getattr(x, 'energy', 100)

100

>>>