import time def cal_time(fn): def inner(num): start_time = time.time() ret = fn(num) # 调用原有函数,将num传递给fn end_time = time.time() print("程序一共运行了%s" % (end_time - start_time) + ‘秒‘) return ret # 返回inner函数的返回值 return inner # 返回内部函数引用 @cal_time def func(n): x = 1 for i in range(1, 10000): x *= i return x # 返回数字10000给调用者 result = func(10) # 相当于:func = cal_time(func) print(result)
原文:https://www.cnblogs.com/yujiemeigui/p/14612165.html