0
点赞
收藏
分享

微信扫一扫

Python 装饰器的嵌套


1. 多层装饰器嵌套

def makeBold(func):
def wrapper(*args, **kwargs):
return "<b>" + func() + "<b>"
return wrapper


def makeItail(func):
def wrapper(*args, **kwargs):
return "<i>" + func() + "<i>"
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


举报

相关推荐

0 条评论