Skip main navigation

New offer! Get 30% off one whole year of Unlimited learning. Subscribe for just £249.99 £174.99. New subscribers only T&Cs apply

Find out more

The ‘Wraps’ decorator

Learn how the *wraps* decorator can be used to preserve the original name and docstring of the original function.
0
[SOFT MUSIC PLAYING]
12.3
Hi, and welcome back to the course. So in the previous cells we defined our say my name decorator and we applied it to the subtracting function. Now something we’ve seen in our closure lectures is that when you return an inner function from an outer function, this inner function is what defines the name and the docstring. So what I mean is when we apply this decorator to our subtract 2b function, right, our subtract 2b function, this wrapper is the function that’s actually returned. And of course inside this wrapper, it calls the subtract 2b function so it behaves like the subtract 2b function. But the point is when we use a decorator, it returns this wrapper function.
56.7
And what that means is that the function you actually get back will have the name wrapper in the docstring of the wrapper. So for example, if we print the name and docstring of the subtract 2b function after we’ve applied our wrapper, notice what the name and the docstring are, right, the name of the function is wrapper - oops sorry. The name of the function is wrapper, and the docstring is the docstring of the wrapper. So just see what prints here, and see that it matches the name and the docstring of the wrapper function of our inner function. Now, that’s not ideal.
96.7
I mean, the problem with that is that any time we use a decorator on a function, we would lose the function’s original name and original docstring - and we don’t really want that. But there’s an easy way around it. We can actually use the wraps decorator to maintain the name and the docstring. And so the first thing we need to do is import it, and it’s from the functools package - or sorry module. So from the functools module - which is part of the built in library so you don’t have to instal anything extra - import wraps, and this is the decorator function. And all we need to do is use that decorator function inside our own decorator function.
145.2
So here’s our say my name decorator function. I’m just defining it again. The only difference is this line. I’m using the wraps decorator on our wrapper function. Notice I’m using the decorator notation that uses the at symbol. And so you put that right above the definition of the function that you want to decorate. And then we’re actually passing an argument to this decorator, which is the func. It is the function that we’ve passed to our decorator. So we passed the original function to our decorator. We also passed that function to the wraps decorator, and we’re using the wraps decorator on our wrapper function. Run this cell.
192.1
And now let’s use the updated say my name decorator on our subtract 2b function. So I’m actually defining subtract 2b function again, and this time I am applying the decorator. And now when we print the name and the documentation, you can see it’s the original name, and the original docstring of the function. So this is more ideal because now we can decorate our functions, and we can maintain the name and docstring. All right. Great job, and I’ll see you in the next lecture.

The wraps decorator can be used to preserve the original name and docstring of the original function.

Without the wraps decorator, these would be replaced which is not ideal. Now, we can use decorators without worrying about changing names and docstrings.

From the closure lessons, we’ve seen that when you return an inner function from an outer function, the inner function is what defines the name of the docstrings. We saw how the decorated functions will take on the docstring and the name of the inner wrapper function. This is because they are the inner wrapper function.

Follow along

The file used in this video is Introducing Decorators.ipynb. If you have not already done so, please download this file from the Downloads section in Step 4.6 – Decorator examples.
Make sure you are able to access it, in order to follow along with the video.

In the next step, we will take a closer look at memoization

This article is from the free online

Intermediate Python

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now