Python update_wrapper()

jjw's avatar
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.

  • 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

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 to WRAPPER_ASSIGNMENTS).
  • updated: A tuple of attribute names to update (defaults to WRAPPER_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_wrapper
def my_decorator(func):
def wrapper(*args, **kwargs):
print(f"Calling {func.__name__}")
return func(*args, **kwargs)
# Update the wrapper to look like the original function
update_wrapper(wrapper, func)
return wrapper
@my_decorator
def greet(name):
"""A simple greeting function."""
return f"Hello, {name}!"
# Check that metadata is preserved
print(f"Function name: {greet.__name__}")
print(f"Function docstring: {greet.__doc__}")

Here is the output:

Function name: greet
Function docstring: A simple greeting function.

Codebyte Example

This codebyte example shows the difference between using and not using update_wrapper():

Code
Output
Loading...

All contributors

Contribute to 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