Created
July 28, 2012 18:58
-
-
Save JamesMcMahon/3194418 to your computer and use it in GitHub Desktop.
Decorate Python Test
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
# level one | |
def foo(fn): | |
def inner(): | |
return 'foo' + fn() | |
return inner | |
@foo | |
def bar(): | |
return 'bar' | |
# level two | |
def pre(s): | |
def decorate(fn): | |
def inner(): | |
return s + fn() | |
return inner | |
return decorate | |
@pre('cookie') | |
def post(): | |
return 'monster' | |
# level two modified | |
def pre2(s): | |
print s | |
def decorate(fn): | |
return fn | |
return decorate | |
@pre2('cookie') | |
def post2(): | |
return 'monster' | |
# level three, pick up your weapons and fight | |
def level1(s): | |
print s | |
def level2(s2): | |
print s2 | |
def decorator(fn): | |
return fn | |
return decorator | |
return level2 | |
gatekeeper = level1('I am the gatekeeper') | |
@gatekeeper('I am the keymaster') | |
def keymaster(): | |
return 'omg' | |
print bar() | |
print post() | |
print post2() | |
print keymaster() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment