Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timing decorator is raising "'NoneType' object is not callable" exception

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
like image 361
Ilya Peterov Avatar asked Nov 27 '25 04:11

Ilya Peterov


1 Answers

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
like image 175
NPE Avatar answered Nov 29 '25 18:11

NPE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!