🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The functools.update_wrapper function in Python's functools module is used to update a wrapper function to look more like the wrapped function. This is particularly useful when writing decorators, as it helps preserve the metadata of the original function.
Table of Contents
- Introduction
functools.update_wrapperFunction Syntax- Examples
- Basic Usage
- Using with a Simple Decorator
- Customizing the Wrapper
- Real-World Use Case
- Conclusion
Introduction
When creating decorators, the metadata of the original function (such as its name, docstring, and module) is often lost. The functools.update_wrapper function helps copy these attributes from the original function to the wrapper function, making debugging and introspection easier.
functools.update_wrapper Function Syntax
Here is how you use the functools.update_wrapper function:
import functools
functools.update_wrapper(wrapper, wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)
Parameters:
wrapper: The function to be updated.wrapped: The original function.assigned: Optional. A tuple specifying which attributes of the original function are assigned directly to the wrapper function (default isWRAPPER_ASSIGNMENTSwhich includes__module__,__name__,__qualname__,__annotations__, and__doc__).updated: Optional. A tuple specifying which attributes of the wrapper function are updated with the corresponding attributes from the original function (default isWRAPPER_UPDATESwhich includes__dict__).
Returns:
- The wrapper function with updated attributes.
Examples
Basic Usage
Update a simple wrapper function to preserve the metadata of the original function.
Example
import functools
def original_function():
"""This is the original function."""
pass
def wrapper_function():
pass
functools.update_wrapper(wrapper_function, original_function)
print(wrapper_function.__name__) # Output: original_function
print(wrapper_function.__doc__) # Output: This is the original function.
Using with a Simple Decorator
Create a decorator that uses functools.update_wrapper to preserve the metadata of the decorated function.
Example
import functools
def my_decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
print("Wrapper function")
return func(*args, **kwargs)
return wrapper
@my_decorator
def my_function():
"""This is my function."""
print("Original function")
print(my_function.__name__) # Output: my_function
print(my_function.__doc__) # Output: This is my function.
Customizing the Wrapper
Customize which attributes are updated by update_wrapper.
Example
import functools
def original_function():
"""This is the original function."""
pass
def wrapper_function():
pass
functools.update_wrapper(wrapper_function, original_function, assigned=('__doc__',))
print(wrapper_function.__name__) # Output: wrapper_function
print(wrapper_function.__doc__) # Output: This is the original function.
Real-World Use Case
Logging Decorator
Create a logging decorator that preserves the metadata of the decorated function.
Example
import functools
def logging_decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
print(f"Calling {func.__name__}")
return func(*args, **kwargs)
return wrapper
@logging_decorator
def compute(x, y):
"""Compute the sum of x and y."""
return x + y
print(compute(2, 3)) # Output: Calling compute
# 5
print(compute.__name__) # Output: compute
print(compute.__doc__) # Output: Compute the sum of x and y.
Conclusion
The functools.update_wrapper function is used for creating decorators that preserve the metadata of the original function. It helps maintain the integrity of function attributes such as the name, docstring, and module, which is important for debugging and introspection. Proper usage can enhance the readability and maintainability of decorated functions.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment