函数的装饰器和类的装饰器
函数的装饰器 : (1),被装饰的函数没有返回值: 1 def decorator(func): 2 def wrapper(arg): 3 t1 = time.perf_counter() 4 func(arg) 5 print(time.perf_counter()-t1) 6 return wrapper 7 8 @decorator 9 def func(arg): #没有返回值 10 i = 1 11 while(1): 12 i+=1 13 if i>1000: 14 print(arg) 15 break 16 17 18 if __name__ =="__main__": 19 import time 20 func("I am done") 21 22 ''' 23 输出: 24 I am done 25 0.00021631981540709086 26 ''' (2),被装饰的函数有返回值: 1 def decorator(func): 2 def wrapper(arg): 3 t1 = time.perf_counter() 4 i = func(arg) 5 print(time.perf_counter()-t1) 6 return i 7 return wrapper 8 9 @decorator 10 def func(arg): #有返回值 11