Ily*_*rov 3 python python-3.x python-decorators
我有计时功能和我的主要功能.当我只使用main函数它运行正常,但当我使用计时功能作为装饰器时,它会引发异常.
定时功能码:
def timing(function):
import time
t = time.time()
function()
t = time.time() - t
print('Program has been running for {} seconds.'.format(t))
Run Code Online (Sandbox Code Playgroud)
我这样使用它:
@timing
def main():
#some code
Run Code Online (Sandbox Code Playgroud)
装饰器需要返回装饰函数:
def timing(function):
def wrapped():
import time
t = time.time()
function()
t = time.time() - t
print('Program has been running for {} seconds.'.format(t))
return wrapped
@timing
def main():
# some code
Run Code Online (Sandbox Code Playgroud)