在Python中堆叠装饰器

时间:2013-07-01 18:27:05

标签: python decorator

因此...

这有效

def makeBold(fn):
    def wrapped():
        return '<b>'+fn()+'</b>'
    return wrapped
@makeBold
def sayhello():
    return 'Ey yo wassup'

并产生预期的 Ey yo wassup

然而,这不起作用

def makeBold(fn):
    def wrapped():
        return '<b>'+fn()+'</b>'
    return wrapped

def makeItalic(fn):
    def wrapped():
        return '<i>'+fn()+'</i>'

@makeItalic
@makeBold
def sayhello():
    return 'Ey yo wassup'

哪一个都产生了这个漂亮的NoneType错误...

我认为它会产生类似 Ey yo wassup

的东西

思想?

1 个答案:

答案 0 :(得分:0)

def makeBold(fn):
    def wrapped():
        return '<b>'+fn()+'</b>'
    return wrapped

def makeItalic(fn):
    def wrapped():
        return '<i>'+fn()+'</i>'
    return wrapped

@makeItalic
@makeBold
def sayhello():
    return 'Ey yo wassup'