Skip to content

Instantly share code, notes, and snippets.

Created December 12, 2012 05:50
Show Gist options
  • Save anonymous/54b26d120ffb8a2412e2 to your computer and use it in GitHub Desktop.
Save anonymous/54b26d120ffb8a2412e2 to your computer and use it in GitHub Desktop.
def mydec(c, f):
def r_dec(function):
def wrapper(*args, **kwargs):
print "## PRE-RUN CODE HERE ##"
getattr(c(), f)(*args[1:], **kwargs)
print "## POST-RUN CODE HERE ##"
return wrapper
return r_dec
class B:
def func1(self, a, b, c):
print "B::func1 %s %s %s" % (a, b, c)
class A:
@mydec(B, "func1")
def func1(self, a, b, c):
print "A::func1 %s %s %s" % (a, b, c)
print "Calling original B.func1 (unwrapped)"
y = B()
y.func1(1, 2, 3)
print "---"
print "Calling A.func1 wrapping B.func1"
x = A()
x.func1(1, 2, 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment