Last active
April 6, 2017 22:29
-
-
Save AndresMWeber/90976957b9506e1aebcb190ea19df762 to your computer and use it in GitHub Desktop.
Trying to delegate but inherit.
This file contains hidden or 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
# 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works now...looks shitty though but I guess.