之前学的装饰器有挺多不懂得,今天又重新看了一下,才明白装饰器的实质就是闭包
import time def inner(x): def wapper(*args,**kwargs): s_time = time.time() x(*args,**kwargs) ss_time = time .time() print(ss_time - s_time) return wapper @inner def func(*args,**kwargs): print(*args,**kwargs) time.sleep(2) func(4,5,6,'hhh','fghjfg','hfjgh',{'asfd':65656})
这是一个代码效率检测的小程序,接下来上一个装饰器的一般简易形式
def inner(func): def warpper(*args,**kwargs): func(*args,**kwargs) return warpper @inner def num_sum(*args,**kwargs): print(*args,**kwargs)