Created
April 29, 2013 06:39
-
-
Save sidchilling/5480034 to your computer and use it in GitHub Desktop.
How to use more than one decorator to decorate a function
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
# More than one decorator on a function | |
# A decorator to make text bold | |
def make_bold(func): | |
def wrapper_1(): | |
return '<b>%s</b>' %(func()) | |
return wrapper_1 | |
# A decorator to make text italic | |
def make_italic(func): | |
def wrapper_2(): | |
return '<i>%s</i>' %(func()) | |
return wrapper_2 | |
@make_bold | |
@make_italic | |
def tell(): | |
return 'hello' | |
print tell() | |
# Order in which decorators are written MATTERS | |
@make_italic | |
@make_bold | |
def scream(): | |
return 'Oye!' | |
print scream() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment