什么是装饰器:用于拓展原来函数功能的一种函数,目的是在不改变原函数名(或类名)的情况下,给函数增加新的
装饰器作用于最近的函数
在不修改 demo 函数的情况下,扩展 demo 功能,每次调用只能用 wrapper_demo
1 def demo(): 2 print('this is demo !') 3 4 5 def wrapper_demo(func): 6 print('this is wrapper demo start !') 7 func() 8 print('this is wrapper demo end !') 9 10 wrapper_demo(demo)
在不修改 demo 调用方式的情况下,扩展 demo 功能
1 def demo(): 2 print('this is demo !') 3 4 5 def wrapper_demo(func): 6 7 def inner(): 8 print('this is wrapper demo start !') 9 10 func() 11 12 print('this is wrapper demo end !') 13 return inner 14 15 demo = wrapper_demo(demo) # 每次调用都要写一遍 16 demo()
使用语法糖装饰函数,代替 demo = wrapper_demo(demo)
1 def wrapper_demo(func): 2 3 def inner(): 4 print('this is wrapper demo start !') 5 6 func() 7 8 print('this is wrapper demo end !') 9 return inner 10 11 12 @wrapper_demo # 代替 demo = wrapper_demo(demo) 13 def demo(): 14 print('this is demo !') 15 16 17 demo()
带 return 的 demo 函数
1 def wrapper_demo(func): 2 3 def inner(): 4 print('this is wrapper demo start !') 5 6 ret = func() 7 8 print('this is wrapper demo end !') 9 return ret 10 return inner 11 12 13 @wrapper_demo # 代替 demo = wrapper_demo(demo) 14 def demo(): 15 print('this is demo !') 16 return 'Hello World' 17 18 ret = demo() 19 print(ret)
带参数的 demo,万能参数的装饰器
1 def wrapper_demo(func): 2 3 def inner(*args, **kwargs): 4 print('this is wrapper demo start !') 5 6 ret = func(*args, **kwargs) 7 8 print('this is wrapper demo end !') 9 return ret 10 return inner 11 12 13 @wrapper_demo 14 def demo1(name): 15 print(f"{name} 进化!") 16 return '进化形态---->暴龙兽!' 17 18 19 @wrapper_demo 20 def demo2(name, evolution_name): 21 print(f"{name} 超进化!") 22 return f"超进化形态---->{evolution_name}!" 23 24 25 ret = demo1('亚古兽') 26 print(ret) 27 28 print('-' * 20) 29 30 ret = demo2('暴龙兽', '机械暴龙兽') 31 print(ret)
多个装饰器
1 def wrapper_demo1(func): 2 3 def inner(*args, **kwargs): 4 print('-----进化 !') 5 6 ret = func(*args, **kwargs) 7 8 print('-----超级火焰弹 !') 9 return ret 10 return inner 11 12 13 def wrapper_demo2(func): 14 15 def inner(*args, **kwargs): 16 print('+++++超进化开始 !') 17 18 ret = func(*args, **kwargs) 19 20 print('+++++盖亚能量炮 !') 21 return ret 22 return inner 23 24 25 def wrapper_demo3(func): 26 27 def inner(*args, **kwargs): 28 print('=====究极进化开始 !') 29 30 ret = func(*args, **kwargs) 31 32 print('=====战斗龙卷风 !') 33 return ret 34 return inner 35 36 37 def demo1(name, big_recruit): 38 print(f"究极形态---->{name} !") 39 return f"大招---->{big_recruit}!" 40 41 42 @wrapper_demo1 # wrapper_demo1(wrapper_demo2(wrapper_demo3(demo2))) 43 @wrapper_demo2 # wrapper_demo2(wrapper_demo3(demo2)) 44 @wrapper_demo3 # wrapper_demo3(demo2) 45 def demo2(name, evolution_name): 46 print(f"{name} 究极进化!") 47 return f"究极进化形态---->{evolution_name}!" 48 49 50 ret = demo2('机械暴龙兽', '战斗暴龙兽') 51 print(ret) 52 53 print('-' * 50) 54 55 ret = wrapper_demo1(wrapper_demo2(wrapper_demo3(demo1)))('战斗暴龙兽', '战斗龙卷风') 56 print(ret)
不修改 demo.__name
from functools import wraps def wrapper_demo1(func): @wraps(func) # 不修改 demo.__name__ def inner(*args, **kwargs): print('-----进化 !') ret = func(*args, **kwargs) print('-----超级火焰弹 !') return ret return inner @wrapper_demo1 def demo2(name, evolution_name): print(f"{name} 究极进化!") return f"究极进化形态---->{evolution_name}!" print(demo2.__name__)
带参数的装饰器
1 name1 = '加布兽' 2 name2 = '加鲁鲁兽' 3 4 def evolution(name, new_name): 5 def wrapper_demo(func): 6 7 def inner(*args, **kwargs): 8 print(f"{name} 进化 {new_name} !") 9 10 ret = func(*args, **kwargs) 11 12 print(f"极零冷冻波 !") 13 return ret 14 return inner 15 16 return wrapper_demo 17 18 19 @evolution(name1, name2) 20 def demo(name, evolution_name): 21 print(f"{name} 究极进化---->{evolution_name} !") 22 23 24 demo(name2, '钢铁加鲁鲁兽')