A decorator does not "replace" the function is return value; it wraps the function. To ensure the caller receives the original function is return value, the wrapper function inside the decorator must explicitly return the result of calling the original function.
If you forget this return, the decorated function will return None, and the original return value will be lost.
Example
import functools
def my_decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
print("Before function runs")
result = func(*args, **kwargs) # save the original return value
print("After function runs")
return result # key: return the value
return wrapper@my_decorator
def add(a, b):
print("Inside add")
return a + bvalue = add(2, 3)
print("Result:", value)
Output:
Before function runs
Inside add
After function runs
Result: 5
If you remove return result from wrapper, the final Result becomes None, because the wrapper function implicitly returns None.
Incorrect Example (Common Mistake)
def bad_decorator(func):
def wrapper(*args, **kwargs):
print("Before")
func(*args, **kwargs) # called, but no return
print("After")
# implicit return None
return wrapper@bad_decorator
def add(a, b):
return a + bprint(add(2, 3)) # prints None, not 5
Key Points
The decorator itself returns wrapper (the function object), not the result of calling the function.
wrapper must return func(...) to pass the original return value through.