Blog Entry 12 years, 9 months ago

A really good spot for Python decorator

When searching for Python decorator, I found these

When searching for Python decorator, I found these:

PythonWiki: http://wiki.python.org/moin/PythonDecorators

PEP 318 -- Decorators for Functions and Methods: http://www.python.org/dev/peps/pep-0318/

But I especially like this one from SO: http://stackoverflow.com/questions/739654/understanding-python-decorators

Inside that thread, the first answer is very short and easy to read:

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>

But the latter (insanely long!) answer of e-satis really is the good one. Check it out here:

http://stackoverflow.com/questions/739654/understanding-python-decorators

Recent Reads