Skip main navigation

The ‘Wraps’ decorator

Learn how the *wraps* decorator can be used to preserve the original name and docstring of the original function.

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