Home > DeveloperSection > Forums > How can I make a chain of function decorators in Python?
ezra heywood
ezra heywood

Total Post:145

Posted on    May-11-2015 12:21 AM

 Python Python 

 1 Reply(s)
 727  View(s)
Rate this:
How can I make two decorators in Python that would do the following?
def say():
   return "Hello"
which should return
I'm not trying to make HTML this way in a real application, just trying to understand how decorators and decorator chaining works.

Mayank Tripathi
Mayank Tripathi

Total Post:397

Posted on    May-11-2015 8:22 AM

Check out the documentation to see how decorators work. Here is what you asked for:
def makebold(fn):
    def wrapped():
        return "<b>" + fn() + "</b>"
    return wrapped
def makeitalic(fn):
    def wrapped():
        return "<i>" + fn() + "</i>"
    return wrapped
def hello():
    return "hello world"
print hello() ## returns <b><i>hello world</i></b>

Don't want to miss updates? Please click the below button!

Follow MindStick