python,装饰器带参数,原理
1 import time 2 3 # 函数装饰器传参 4 def zhuang1(hao): 5 def zhuang2(func1): 6 def funczhuang(*args, **kw): 7 print(time.time()) 8 print(hao) 9 print("第三层") 10 return func1(*args, *kw) 11 12 print("第二层+可套代码") 13 return funczhuang 14 15 print('第一层+可套代码') 16 return zhuang2 17 # 不用装饰器实现 18 def test(): 19 time.sleep(1) 20 pass 21 22 test1= zhuang1("J") 23 test = test1(test) 24 test() 25 26 print("*"*100) 27 @zhuang1("Z") 28 def test1(): 29 time.sleep(1) 30 pass 31 32 33 test1()
注 :带参数装饰器,在原有闭包的前提下,再加一层