Skip to content

Instantly share code, notes, and snippets.

@zxkletters
Created September 30, 2013 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zxkletters/6758791 to your computer and use it in GitHub Desktop.
Save zxkletters/6758791 to your computer and use it in GitHub Desktop.
demo of decorator in python
"""
Created on 2013-4-8
@author: xiaoke.zhangxk
"""
def blue_box(func):
def wrapped():
print "~~ blue ~~~"
func()
print "~~ blue ~~"
return wrapped
def red_box(func):
def wrapped():
print "** red **"
func()
print "** red **"
return wrapped
def box():
print " i am a box."
box()
print "-----------------"
decorated = red_box(blue_box(box))
decorated()
print "-----------------"
@blue_box
@red_box
def new_box():
print "i am another box."
new_box()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment