Functools: functools.wraps

Let's talk about functool tools and their functions.

Overview of wraps tool

There is a little known tool that we wanted to cover in this lesson. It is called wraps and it too is a part of the functools module. We can use wraps as a decorator to fix docstrings and names of decorated functions. Why does this matter? This sounds like a weird edge case at first, but if we are writing an API or any code that someone other than ourself will be using, then this could be important. The reason is that when we use Python’s introspection to figure out someone else’s code, a decorated function will return the wrong information. Let’s look at a simple example that we have dubbed Python file decorum (below):

Get hands-on with 1200+ tech skills courses.