all Python 3functoolsterm
functools.wraps
decorators
Copies the metadata of the wrapped function (__name__, __doc__, __annotations__) onto the wrapper. A must when writing decorators.
Syntax
@functools.wraps(wrapped)
Skip it and your decorated function starts lying about itself: help() and __name__ report wrapper, doctests stop being collected, and anything keyed by function name — docs generators, registries — quietly breaks. Note that wraps only repairs introspection: the callable is still wrapper(*args, **kwargs); inspect.signature merely follows the __wrapped__ link it sets to report the original signature.
Examples
from functools import wraps
def my_decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
@my_decorator
def greet(name):
"""Say hello."""
return f'Hello, {name}'
print(greet.__name__) # → greet
print(greet.__doc__) # → Say hello.
print(greet('Alice')) # → Hello, Alice
print(greet.__wrapped__) # → ? repr функции содержит адрес
See also
Open in the full glossarydocs.python.orgReport an inaccuracy