functools.wraps
Decorators replace the wrapped function. Use @functools.wraps(func) to preserve the original function's name, docstring, and other attributes.
Decorators replace the wrapped function. Use @functools.wraps(func) to preserve the original function's name, docstring, and other attributes.
import functools
def my_decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
@my_decorator
def greet():
"""Say hello."""
return "Hello"
print(greet.__name__) # greet (not wrapper)
Always use @functools.wraps in production decorators.