Last active
August 13, 2016 18:16
-
-
Save yuribak/536ba11c59d04fae0452c4ad5a4a4578 to your computer and use it in GitHub Desktop.
Auto call to super method when overriding
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
class Base(object): | |
def __new__(cls, *args, **kwargs): | |
obj = super(Base, cls).__new__(cls, *args, **kwargs) | |
tmpf = obj.f | |
def new_f(*args, **kwargs): | |
super(cls, obj).f(*args, **kwargs) | |
return tmpf(*args, **kwargs) | |
obj.f = new_f | |
return obj | |
def f(self): | |
print 'base f' | |
class Sub(Base): | |
def f(self): | |
print "sub f" | |
if __name__ == '__main__': | |
t = Sub() | |
t.f() | |
# output: | |
# > base f | |
# > sub f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment