-
-
Save viveksoundrapandi/ebd6345aeccde58296f03782766a3ffa to your computer and use it in GitHub Desktop.
forwardable getattr for medium
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, name): | |
# EX: delegates = [("q", "enqueue", "append")] | |
#iterate through to delegate items | |
for attr in self.delegates: | |
#check if the current lookedup attribute is in any of the delegates | |
if name == attr[1] and hasattr(getattr(self, attr[0]), attr[2]): | |
#delegate the call to composed object | |
return getattr(getattr(self, attr[0]), attr[2]) | |
#raise AttributeError to mimick system default | |
raise AttributeError("'{}' object has no attribute '{}'".format(type(self).__name__, name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment