1
def makebold(fn):
    def wrapped():
        return "<b>" + fn() + "</b>"
    return wrapped

def message():
    return "TasPython Guide"

message = makebold(message)
print(message())

Well, I have many questions about the above code...

1st: why inside the wrapped() function we use the fn variable as a function? Shouldn't we use fn instead of fn() ?

2nd: why message = makebold(message) and not message = makebold(message())

3rd: why print(message())gives me this result???

I would expect something like: print(makebold(message())

Thank you guys!

midkin
  • 1,503
  • 2
  • 18
  • 23
  • 2
    Because `makebold` is a *decorator*. Read the question I duped you to, especially the highest-voted answer. – Martijn Pieters Feb 02 '15 at 20:16
  • 1
    It seems you need to learn about something called decorators.Here you have a pretty neat and concise guide to understand it. Go straight to chapter no. 6. http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/ – peluzza Feb 02 '15 at 20:47
  • 2
    Thanks @peluzza ! I 'll read it right after am done with the answers of Martinj Pieters! – midkin Feb 02 '15 at 20:58

0 Answers0