Timing decorator is raising “'NoneType' object is not callable” exception

本秂侑毒 提交于 2020-01-14 19:52:18

问题


I have i timing function and my main function. When i use only main function it runs fine, but when i use timing function as a decorator it raises an exception.

Timing function code:

def timing(function):
    import time
    t = time.time()
    function()
    t = time.time() - t
    print('Program has been running for {} seconds.'.format(t))

I use it like this:

@timing
def main():
    #some code

回答1:


The decorator needs to return the decorated function:

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


来源:https://stackoverflow.com/questions/25877767/timing-decorator-is-raising-nonetype-object-is-not-callable-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!