🎓 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.wraps function in Python's functools module is a decorator that updates the 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.wrapsFunction Syntax- Examples
- Basic Usage
- Using with a Simple Decorator
- Customizing Attributes
- 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.wraps decorator helps copy these attributes from the original function to the wrapper function, making debugging and introspection easier.
functools.wraps Function Syntax
Here is how you use the functools.wraps function:
import functools
@functools.wraps(wrapped, assigned=functools.WRAPPER_ASSIGNMENTS, updated=functools.WRAPPER_UPDATES)
def wrapper_decorator(wrapper):
# Wrapper implementation
pass
Parameters:
wrapped: The original function.assigned: Optional. A tuple specifying which attributes of the original function are assigned directly to the wrapper function (default isfunctools.WRAPPER_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 isfunctools.WRAPPER_UPDATESwhich includes__dict__).
Returns:
- A decorator that updates the wrapper function with the metadata of the wrapped function.
Examples
Basic Usage
Update a simple wrapper function to preserve the metadata of the original 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.
Using with a Simple Decorator
Create a decorator that uses functools.wraps 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 Attributes
Customize which attributes are updated by wraps.
Example
import functools
def my_decorator(func):
@functools.wraps(func, assigned=('__doc__',))
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: wrapper
print(my_function.__doc__) # Output: This is my function.
Real-World Use Case
Timing Decorator
Create a timing decorator that preserves the metadata of the decorated function.
Example
import functools
import time
def timing_decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
print(f"Execution time: {end_time - start_time} seconds")
return result
return wrapper
@timing_decorator
def compute(x, y):
"""Compute the sum of x and y."""
time.sleep(1)
return x + y
print(compute(2, 3)) # Output: Execution time: 1.0 seconds
# 5
print(compute.__name__) # Output: compute
print(compute.__doc__) # Output: Compute the sum of x and y.
Conclusion
The functools.wraps 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