Created
January 31, 2017 16:02
-
-
Save jsbueno/29a5fddf9374001483b4f03d31e5af2f to your computer and use it in GitHub Desktop.
Attempt to create a decorator to automatically call super - failled attempt.
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
def pre_super(func): | |
values = {} | |
try: | |
class Mutant: | |
def wrapper(*args, **kw): | |
getattr(__class__, func.__name__)(*args, **kw) | |
return func(*args, **kw) | |
values["w"] = wrapper | |
raise ValueError # Abort the filling up of wrapper's __class__ cell | |
except ValueError: | |
pass | |
return values["w"] | |
class A: | |
def a(self): | |
print("class A") | |
class B: | |
@pre_super | |
def a(self): | |
print("class B") | |
# At this point | |
# The wrapper function has an empty cell, referenced by a __class__ freevar | |
# but cPython's type does not fill it up with "B". | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment