68. Function Wrapping with wraps
1. Basic Usage of functools.wraps
functools.wrapsimport functools
def simple_decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
@simple_decorator
def example_function():
"""This is a decorated function."""
return "Hello"
print(example_function.__name__) # Output: example_function
print(example_function.__doc__) # Output: This is a decorated function.2. Preserving Metadata with Multiple Decorators
3. Function Signature Preservation
4. Handling Arguments with functools.wraps
functools.wrapsOutput:
5. Preserving Function's Docstring
6. Preserving Function's Original Return Value
Output:
7. Example with Arguments and Keyword Arguments
Output:
8. Debugging with functools.wraps
functools.wrapsOutput:
9. Preserving Function Attributes
10. Dynamic Function Wrapping with functools.wraps
functools.wrapsLast updated