Created
May 28, 2011 20:01
-
-
Save gennad/997175 to your computer and use it in GitHub Desktop.
Decorators
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 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