断点测试装饰器

def timer(func):
def wrapper(*args,**kwargs):#默认可以输入任何参数
# print(func)#嵌套的作用域
strat_time = time.time()
res = func(*args,**kwargs)#就是在运行test(),赋值给func(),实际会赋值给test函数的一个返回值
stop_time = time.time()
print('运行时间是%s'%(stop_time-strat_time))
return res#设定返回值
return wrapper
import time
@timer#test = timer(test)作用是对函数进行计时
def test(name,age):
time.sleep(3)
print('test函数运行完毕,【名字是】:%s,【年龄是】:%s'%(name,age))
return 1
# timer(test)#返回的是wrapper的地址
# test = timer(test)
res1 = test('alex','18')#就是在运行wrapper
print(res1)
Win a contest, win a challenge
原文地址:https://www.cnblogs.com/pandaboy1123/p/8462594.html