The temporary decorator raises the "Optional" NoneType "object is not a throwable" exception

I have a sync function and my main function. When I only use the main function it works fine, but when I use the sync function as a decorator an exception is thrown.

Sync 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

      

+3


source to share


1 answer


The decorator needs to return a 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

      

+5


source







All Articles