Skip to content

Instantly share code, notes, and snippets.

@xydinesh
Created June 13, 2014 14:41
Show Gist options
  • Save xydinesh/1862474ee7a6f0c62a5c to your computer and use it in GitHub Desktop.
Save xydinesh/1862474ee7a6f0c62a5c to your computer and use it in GitHub Desktop.
Implement python decorators usind classes.
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