Skip to content

Instantly share code, notes, and snippets.

@viveksoundrapandi
Last active August 14, 2019 13:40
Show Gist options
  • Save viveksoundrapandi/ebd6345aeccde58296f03782766a3ffa to your computer and use it in GitHub Desktop.
Save viveksoundrapandi/ebd6345aeccde58296f03782766a3ffa to your computer and use it in GitHub Desktop.
forwardable getattr for medium
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