Created
July 10, 2012 13:12
-
-
Save Midnighter/3083155 to your computer and use it in GitHub Desktop.
Redirecting attribute access in a compound class with __getattr__
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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