Monday, February 2, 2015

Overview

Decorators allow you to modify a function when it gets called. It’s used for things like memoization that keeps track of return statements. You can find more information here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def makebold(fn):
    return lambda: "<b>" + fn() + "</b>"

def makeitalic(fn):
    return lambda: "<i>" + fn() + "</i>"

@makebold
@makeitalic
def hello():
    return "hello world"

hello()
# "<b><i>hello world</i></b>"
# "<b>" + ("<i>" + fn() + "</i>") + "</b>"


print hello() ## returns <b><i>hello world</i></b>

Random Posts