Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
The __getattr__ method
intercepts attribute qualifications. More specifically,
it’s called with the attribute name as a string whenever you try to
qualify an instance with an undefined
(nonexistent) attribute name. It is not called if Python can find the
attribute using its inheritance tree search procedure. Because of its
behavior, __getattr__ is useful as
a hook for responding to attribute requests in a generic fashion. For
example:
>>>class empty:...def __getattr__(self, attrname):...if attrname == "age":...return 40...else:...raise AttributeError(attrname) ... >>>X = empty()>>>X.age40 >>>X.name...error text omitted...AttributeError: name