What does functools.wraps do?

functools.wraps is a decorator that can be used to modify a function or method by updating its metadata. It is typically used in combination with other decorators to preserve the metadata of the decorated function. Here is an example of how to use functools.wraps to decorate a function:

import functools

def my_decorator(f):
    @functools.wraps(f)
    def wrapper(*args, **kwds):
        print("Calling decorated function")
        return f(*args, **kwds)
    return wrapper

@my_decorator
def example():
    """Docstring"""
    print("Called example function")

print(example.__name__) #example
print(example.__doc__) #Docstring

Watch a course Python - The Practical Guide

Here functools.wraps take the function f as input and copy its attributes like __name__, __doc__ to the wrapper function. This is useful because in the example the decorated function example is wrapped in the wrapper function and the wrapper function will have the attributes of the original function.