Python 装饰器的嵌套

Python 装饰器的嵌套,第1张

1. 多层装饰器嵌套
def makeBold(func):
    def wrapper(*args, **kwargs):
        return "" + func() + ""
    return wrapper


def makeItail(func):
    def wrapper(*args, **kwargs):
        return "" + func() + ""
    return wrapper


@makeItail
def test1():
    return "hello world"

@makeBold
def test2():
    return "hello world"

@makeBold
@makeItail
def test3():
    return "hello world"

print(test1())
print(test2())
print(test3())
2. 输出结果
C:\Users\HuJun\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/HuJun/PycharmProjects/pythonProject/daily_tesy/嵌套装饰器.py
<i>hello world<i>
<b>hello world<b>
<b><i>hello world<i><b>

Process finished with exit code 0

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/867819.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-12
下一篇 2022-05-12

发表评论

登录后才能评论

评论列表(0条)