如何在Python中制作函数装饰器链?
装饰器是“包装器”,使我们可以在装饰的函数之前和之后执行代码,而无需修改函数本身。
示例
可以将给定代码包装在装饰器链中,如下所示。
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()输出结果
C:/Users/nhooo1/~.py <b><i>hello world</i></b>
如果按照下面给出的链接执行该html代码,我们将获得粗体和斜体“helloworld”输出
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短