Skip to content

Instantly share code, notes, and snippets.

@rasmusto
Last active August 29, 2015 14:07
Show Gist options
  • Save rasmusto/42a20cb8165649fc0632 to your computer and use it in GitHub Desktop.
Save rasmusto/42a20cb8165649fc0632 to your computer and use it in GitHub Desktop.
def decorator(f):
def f_new():
print "pre"
f()
print "post"
try:
f.decorated
return f
except AttributeError:
f_new.decorated=True
return f_new
@decorator
def a():
print "a"
a()
# pre
# a
# post
@decorator
def b():
print "b"
a()
b()
# pre
# b
# pre
# a
# post
# post
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment