In my opinion, this would be the most Pythonic way to handle it, although and because it makes your question moot. Note that this defines __getattr__()
instead of __getattribute__()
, because doing so means it only has to deal with the "special" attributes being kept in the internal dictionary.
def __getattr__(self, name):
"""Only called when an attribute lookup in the usual places has failed"""
try:
return self.my_dict[name]
except KeyError:
raise AttributeError("some customized error message")