Skip to content

Instantly share code, notes, and snippets.

@jsbueno
Created January 31, 2017 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsbueno/29a5fddf9374001483b4f03d31e5af2f to your computer and use it in GitHub Desktop.
Save jsbueno/29a5fddf9374001483b4f03d31e5af2f to your computer and use it in GitHub Desktop.
Attempt to create a decorator to automatically call super - failled attempt.
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