Created
August 10, 2017 01:48
-
-
Save samvarankashyap/6175eb880fcd02ee88171389d55c79bd to your computer and use it in GitHub Desktop.
Wrapper functions implementation in python
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 wrap(pre, post): | |
def decorate(func): | |
def call(*args, **kwargs): | |
pre(func, *args, **kwargs) | |
result = func(*args, **kwargs) | |
post(func, *args, **kwargs) | |
return result | |
return call | |
return decorate | |
def trace_in(func, *args, **kwargs): | |
print args | |
print kwargs | |
print "Entering function", func.__name__ | |
def trace_out(func, *args, **kwargs): | |
print args | |
print kwargs | |
print "Leaving function", func.__name__ | |
class testclass: | |
def __init__(self): | |
pass | |
@wrap(trace_in, trace_out) | |
def calc(self, x, y): | |
return x + y | |
tc = testclass() | |
print tc.calc(1, 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment