This is what the __getattr__
special method is for:
def __getattr__(self, attrname):
# Only called if the other ways of accessing the attribute fail.
try:
return self._content[attrname]
except KeyError:
raise AttributeError
Note that if for some reason you try to retrieve an unknown attribute when the _content
attribute doesn't exist, the line
return self._content[attrname]
will recursively invoke __getattr__
in an attempt to get the _content
attribute, and that call will call __getattr__
, and so on until stack overflow.