I\'m writing my own container, which needs to give access to a dictionary inside by attribute calls. The typical use of the container would be like this:
dic
Your first example is perfectly fine. Even the official Python docs recommend this style known as EAFP.
Personally, I prefer to avoid nesting when it's not necessary:
def __getattribute__(self, item):
try:
return object.__getattribute__(item)
except AttributeError:
pass # fallback to dict
try:
return self.dict[item]
except KeyError:
raise AttributeError("The object doesn't have such attribute") from None
PS. has_key() has been deprecated for a long time in Python 2. Use item in self.dict instead.