Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decorated function returns "None"

I'm extremely new to python, and i just encountered decorators. I'm still kinda confused by them but i am learning

i was trying to make a decorator that tells me how much time my function took to finish, but apparently when i try to use it on a function that should return something, it just returns "None"

I've seen only a couple of questions talking about this problem but none of them actually helped

Here's my code

import time


def time_it(func):  # Here i make a simple decorator function that should time my decorated function
    def wrapper(*args, **kwargs):
        t1 = time.time()
        func(*args)
        t2 = time.time()
        total = t2 - t1
        print("The function '" + func.__name__ + "' took", str(total)[0:5], "seconds to complete")

    return wrapper


@time_it
def square(nums):  # I make a function that squares every number in a list
    new_list = []
    for n in nums:
        new_list.append(n ** 2)
    return new_list


lis = [f for f in range(200000)]  # i make a list with a range of 200000
print(square(lis))  

sorry for any grammatical errors, i'm not a native english speaker

like image 786
TemporalParadox Avatar asked Aug 06 '26 09:08

TemporalParadox


1 Answers

The problem is that your inner function return value isn't being returned. The change is noted below:

from functools import wraps

def time_it(func):  # Here i make a simple decorator function that should time my decorated function
    @wraps(func)
    def wrapper(*args, **kwargs):
        t1 = time.time()
        ## Note the change on this line -- I now store the return result from the called function 
        result = func(*args, **kwargs)
        t2 = time.time()
        total = t2 - t1
        print("The function '" + func.__name__ + "' took", str(total)[0:5], "seconds to complete")

        ## And then explicitly return the result
        return result

    return wrapper

For the decorator, you need to remember that it's just a closure, with some fancy syntax. You still need to deal with the function return parameters yourself.

A couple of additions:

  • from functools import wraps and @wraps(func)
    • this will create wrap the inner function with some details that exist in the wrapping function. There's a small example in the python docs here: https://docs.python.org/3/library/functools.html
like image 82
Hikash Avatar answered Aug 09 '26 00:08

Hikash



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!