Created
June 13, 2014 14:41
-
-
Save xydinesh/1862474ee7a6f0c62a5c to your computer and use it in GitHub Desktop.
Implement python decorators usind classes.
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
class MyDecorator(object): | |
def __init__(self, f): | |
print "inside init {0}".format(f.__name__) | |
f() | |
def __call__(self): | |
print "inside call {0}".format(__name__) | |
@MyDecorator | |
def afunction(): | |
print "inside afunction" | |
class entryExit(object): | |
def __init__(self, f): | |
self.f = f | |
def __call__(self): | |
print "Entering {0}".format(self.f.__name__) | |
self.f() | |
print "Exit {0}".format(self.f.__name__) | |
@entryExit | |
def func1(): | |
print "inside func1()" | |
@entryExit | |
def func2(): | |
print "inside func2()" | |
if __name__ == "__main__": | |
print "Finished decorating function" | |
afunction() | |
func1() | |
func2() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment