Skip to content

Instantly share code, notes, and snippets.

@gennad
Created May 28, 2011 20:01
Show Gist options
  • Save gennad/997175 to your computer and use it in GitHub Desktop.
Save gennad/997175 to your computer and use it in GitHub Desktop.
Decorators
class myDecorator(object):
def __init__(self, f):
print "inside myDecorator.__init__()"
f() # Prove that function definition has completed
def __call__(self):
print "inside myDecorator.__call__()"
@myDecorator
def aFunction():
print "inside aFunction()"
print "Finished decorating aFunction()"
aFunction()
class entryExit(object):
def __init__(self, f):
self.f = f
def __call__(self):
print "Entering", self.f.__name__
self.f()
print "Exited", self.f.__name__
@entryExit
def func1():
print "inside func1()"
@entryExit
def func2():
print "inside func2()"
func1()
# ---
"""
def exit(f):
def new_f():
print "Entering", f.__name__
f()
print "Exited", f.__name__
return new_f
@exit
def func1():
print "inside func1()"
@exit
def func2():
print "inside func2()"
func1()
func2()
print func1.__name__
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment