Skip to content

Instantly share code, notes, and snippets.

@AndresMWeber
Last active April 6, 2017 22:29
Show Gist options
  • Save AndresMWeber/90976957b9506e1aebcb190ea19df762 to your computer and use it in GitHub Desktop.
Save AndresMWeber/90976957b9506e1aebcb190ea19df762 to your computer and use it in GitHub Desktop.
Trying to delegate but inherit.
# Delegator
class Delegate(object):
def __init__(self):
pass
def name(self):
print 'yo'
class HasDelegate(object):
def __init__(self):
self.delegator = Delegate()
def __getattr__(self, attr):
try:
return getattr(self.delegator, attr)
except AttributeError:
return self.__getattribute__(attr)
class WantsDelegate(HasDelegate):
def __init__(self):
super(self.__class__, self).__init__()
def use_delegate_func(self):
super(self.__class__, self).__getattr__('name')()
b = HasDelegate()
print 'from b, ', b.name()
a = WantsDelegate()
print 'from a, ', a.use_delegate_func()
@AndresMWeber
Copy link
Author

Works now...looks shitty though but I guess.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment