The decorator implementation in python is helpful while implementing add on to a particular class functionality. Here in the below example the html bold and italic properties are added to a string using the decorator implementation.
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
def makebold(fn): | |
def wrapped(): | |
return "<b>" + fn() + "</b>" | |
return wrapped | |
def makeitalic(fn): | |
def wrapped(): | |
return "<i>" + fn() + "</i>" | |
return wrapped | |
@makebold | |
@makeitalic | |
def hello(): | |
return "hello world" | |
print hello() ## returns <b><i>hello world</i></b> |