I've been trying to understand decorators a little better. I'm confused why I'm getting an error using my timer decorator on my factorial function. My factorial function works independently. I'm thinking maybe it's because the decorator is returning something as well as the factorial function. This is the error I'm receiving:
Code for my decorator:
def timer(func):
"""A decorator that prints how long a function took to run.
Args:
func (callable): The function being decorated.
Returns:
callable: The decorated function."""
def wrapper(*args, **kwargs):
# when wrapper is called, get the current time.
t_start = time.time()
# call the decorated function and store the result
result = func(*args, **kwargs)
# get the total time it took to run, and print it.
t_total = time.time() - t_start
print(f"{func.__name__} took {t_total}s")
return wrapper
Code for my quick factorial function:
def factorial(n):
if n == 0:
return 1
else:
return (n * factorial(n-1))
Your wrapper function is not returning anything, thus the error about the unsupported operation between int and NoneType (since a function without return statemet actually returns None). You need to add return result
as last statement to your wrapper function.
Your decorator should look like this:
def timer(func):
def wrapper(*args, **kwargs):
# when wrapper is called, get the current time.
t_start = time.time()
# call the decorated function and store the result
result = func(*args, **kwargs)
# get the total time it took to run, and print it.
t_total = time.time() - t_start
print(f"{func.__name__} took {t_total}s")
return result
return wrapper