Decorator is simply a function that take another function as argument and manipulate it by adding functionalities etc.

def expose(fn):
    if fn.__name__ == 'index': print fn('kamal')
    if fn.__name__ == 'test': print fn(80)
    fn()
 
def html(fn):
    def _wrapper():
        print "<html>"
        print fn('kamal')
        print "</html>"
    return _wrapper
 
def index(name):
    return 'hello %s' % name
 
@expose
@html
def test(param):
    return 'param is %s' % param
 
python/decorator.txt · Last modified: 2007/10/31 01:08 (external edit)
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki