Introduction to Decorators in Python

Dipam Vasani
Towards Data Science
4 min readMar 11, 2020

--

Learn how you can change the behavior of objects

source: https://www.dreamstime.com/photos-images/autumn-scenery.html

Introduction

We are used to passing and returning various data types in and out of a function. It’s time to start passing and returning functions from functions. Let’s take an example.

In this example, we have a function called test that calls one of its two inner functions based on a condition.

However, instead of calling the function, we can also return a reference to the function. The reference can then be used to call the function. Let’s take another example.

In this example, we are returning _inner and not _inner(). This returning of reference and using it later is the basic idea behind a decorator in Python.

Decorators

A decorator is nothing but a wrapper around a function in Python. It takes the function of interest as an argument, performs certain tasks before and after calling it and returns the results.

We pass a reference of say_hello to our decorator which then returns a wrapper. We use this wrapper to call our function.

Now, we don’t want to type all that every time we use decorators. And we don’t have to. Thanks to Python’s syntax sugar we can do the same thing as follows:

Now let’s modify our decorator function a bit. Right now, it’s very limiting. It does not take any arguments and does not return the value returned by our function. Let’s add that.

This right here, is a good template for writing decorator functions. Let’s try some simple examples. These are not the best examples, however, I hope they serve as a good starting point and motivation to try something fancy.

1. Check stats

We’ve seen that normalization is an important step in any data science application. Hence, whenever we normalize something, we might want to check the mean and standard deviation before and after normalizing. For this we can do something like

This can also be used when we normalize images after convolutions to make sure all values are between 0 and 255.

2. Compulsory documentation

You can access a functions documentation using the __doc__ attribute. If you are developing a fancy application that generates automatic documentation, you want to ensure all your code is documented. Let’s try:

Check the inspect library in Python to learn more about how to inspect and play around with live objects in Python.

3. Annotation

Similar to documentation, we can change how annotations behave in Python. When we use annotation in Python, it is usually to ensure that the input and output is of a certain type.

However, we can use the annotations for something else. Maybe we can use the output annotation to typecast the output to the expected data type if it’s not of that type already.

Conclusion:

That will be it for this article. I hope it serves as a good introduction to decorators in Python. Start from here. Learn more advanced use cases of decorators. Learn how to apply them to classes. Learn other libraries and combine them with decorators to build something useful.

Decorators, along with meta-classes (next article) can serve as powerful tools to customize Python the way you want.

~Happy learning

--

--