加上返回值的装饰器

import time
#装饰器的架子如下
def timmer(func):
def wrapper():
start_time = time.time()
res = func() #这一步就是在运行test
stop_time = time.time()
print('程序运行时间是%s' %(start_time - stop_time))
return res
return wrapper

@timmer #相当test = timmer(test)
def test(): #被修饰函数
time.sleep(0.5)
print('test函数运行完毕')
return '这是test 的返回值'

res = test() #就是在运行wrapper
print(res)
原文地址:https://www.cnblogs.com/lhqlhq/p/8744459.html