Python update_wrapper()
Published Aug 27, 2025
Contribute to Docs
The update_wrapper() function from the functools module updates a wrapper function to look like the wrapped function by copying attributes such as __name__, __doc__, and __module__. This is essential for creating decorators that preserve the original function’s metadata, making debugging and introspection much easier.
Syntax
from functools import update_wrapper
update_wrapper(wrapper, wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)
Parameters:
wrapper: The function that is acting as a wrapper (e.g., inside a decorator).wrapped: The original function being wrapped.assigned: A tuple of attribute names to copy (defaults toWRAPPER_ASSIGNMENTS).updated: A tuple of attribute names to update (defaults toWRAPPER_UPDATES).
Return value:
The update_wrapper() function returns the wrapper function itself, after updating it with the attributes of wrapped.
Example
This example demonstrates how update_wrapper() preserves the original function’s metadata when creating a decorator:
from functools import update_wrapperdef my_decorator(func):def wrapper(*args, **kwargs):print(f"Calling {func.__name__}")return func(*args, **kwargs)# Update the wrapper to look like the original functionupdate_wrapper(wrapper, func)return wrapper@my_decoratordef greet(name):"""A simple greeting function."""return f"Hello, {name}!"# Check that metadata is preservedprint(f"Function name: {greet.__name__}")print(f"Function docstring: {greet.__doc__}")
Here is the output:
Function name: greetFunction docstring: A simple greeting function.
Codebyte Example
This codebyte example shows the difference between using and not using update_wrapper():
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours