Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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