装饰器练习

xiaoxiao2021-02-28  129

#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)
转载请注明原文地址: https://www.6miu.com/read-28635.html

最新回复(0)