内置模块 - functools -> wraps
带参数的装饰器:@wraps()
1 import time 2 from functools import wraps 3 4 def time_fun(type): 5 def outter(func): 6 @wraps(func) #可以得到原始函数add的原始信息 7 def inner(*args, **kwargs): 8 time_start = time.time() 9 func(*args, **kwargs) 10 time_end = time.time() 11 print(time_end - time_start) 12 return inner 13 print(type) 14 return outter 15 16 17 @time_fun('ok') 18 # <function time_fun.<locals>.outter.<locals>.inner at 0x00000158C9810BF8> 19 # <function add at 0x000001B9CEBD0AE8> 20 def add(x, y): 21 time.sleep(2) 22 return x+y 23 24 print(add)
浙公网安备 33010602011771号