#Author:donghuiya #高阶函数+嵌套函数=装饰器 import time def timer(func): def deco(*args,**kwargs): start_time=time.time() func(*args,**kwargs) stop_time=time.time() print("the func run time is %s"%(stop_time-start_time)) return deco @timer #test1=timer(test1) 左test1相当于deco def test1(): time.sleep(3) print("this is test1") @timer #要更改deco def test2(name,age): print("test2",name,age) test1() test2("alex",22)
装饰器练习
最新推荐文章于 2021-09-25 16:13:42 发布