Decorators and wraps
Learn in depth →A decorator transforms a function when its definition executes. wraps preserves useful metadata.
@decorator
def function(...): ...Example
from functools import wraps
def traced(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
return fn(*args, **kwargs)
return wrapper
@traced
def total(a, b):
return a + b
print(total(2, 3), total.__name__)Result
5 total
Watch for: Stacked decorators apply from the bottom upward. A synchronous wrapper does not automatically await an async function.
Practise this topic →