def timer(func):#timer做的事情其实是定义了一个deco()函数,并且返回了deco的内存地址
def deco(*args,**kwargs):#deco()做的事情是test1(),并且给出计算时间
import time
start_time = time.time()
func(*args,**kwargs)
stop_time = time.time()
print("the func run time%s"%(stop_time-start_time))
return deco
import time
@timer
def test1():
time.sleep(3)
print("in the test1")
@timer
def test2(name):
print("name:",name)
#将test1传入timer
test1()#调用内存地址(),实际上调用的是deco()
test2("W")
转载于:https://www.cnblogs.com/candyw77/p/10384756.html