Skip to content

Instantly share code, notes, and snippets.

@Midnighter
Created July 10, 2012 13:12
Show Gist options
  • Save Midnighter/3083155 to your computer and use it in GitHub Desktop.
Save Midnighter/3083155 to your computer and use it in GitHub Desktop.
Redirecting attribute access in a compound class with __getattr__
def __getattr__(self, attribute):
"""
Defer any attribute access that was unsuccessful to a compound class.
Introduce a safeguard, for example, when unpickling this class, by raising an AttributeError.
"""
if "compound" in self.__dict__:
self.compound.__getattribute__(attribute)
raise AttributeError("'{0}' object has no attribute '{1}'".format(self.__class__.__name__, attribute))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment