装饰器要装饰的函数的参数个数不确定时,采用弹性参数*args,**kwargs
import time
def timer(func):#timer(test1) func=test1
def deco(*args,**kwargs):
start_time=time.time()
func(*args,**kwargs)#run test1
stop_time=time.time()
print('the func run time is %s'%(stop_time-start_time))
return deco
@timer #加装饰器 test1=timer(test1)
def test1():
time.sleep(3)
print('in the test1')
@timer #加装饰器 test2=timer(test2) =deco test2()=deco()
def test2(name,age):
print("test2:",name,age)
test1()
test2("alex",33)