Last active
December 10, 2015 18:49
-
-
Save jiaaro/4477229 to your computer and use it in GitHub Desktop.
Magical, automatic super class caller in python (a terrible idea)
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
import inspect | |
def magicsuper(*args, **kwargs): | |
frame = inspect.currentframe().f_back | |
first_arg_name = frame.f_code.co_varnames[0] | |
self = frame.f_locals[first_arg_name] | |
super_klass = super(self.__class__, self) | |
return getattr(super_klass, frame.f_code.co_name)(*args, **kwargs) | |
# Example Use: | |
class X(object): | |
def y(self, z): | |
print z | |
class Y(X): | |
def y(self, z): | |
return magicsuper(z) | |
y = Y() | |
y.y('hi mom') # output: "hi mom" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment