Learn Decorators in Python in 5 Steps
By: Kurt F.
Tags: Software Development, Coding, Programming, Python, Data Science

Learn Decorators in Python in 5 Steps

Add additional functionality to one or more functions

Foto von Dids von Pexels

Intro

As we dive deeper into Python, we see many interesting features. One of them is decorators. As it is known, the ability to use many features exactly when we need them takes us forward. So let’s take a closer look at decorators in this article.

1. What are Decorators?

Python has a concept called decorator that allows you to take advantage of the extra functionality quickly. Decorators are structures in Python that allow the use of other functions included in a function separately. This makes it easier to add additional functionality to one or more functions.

When we want to use a simple function more functionally, we encounter two kinds of problems:

The new function is added to the old function. However, in this case, the problem arises of not recalling the original function. When you copy all the old code and create a brand new function that you added later, you will have to recreate the entire functionality. This is also not an ideal way.

So we need a more functional code structure that is both included in the function and has the option to be used when deemed necessary.

Something like the following defines decorators:

2. Which function does it perform?

If you are particularly interested in creating Web pages in Python, you’ll love using this. Because in Flask and Django, which is a more popular frame than Flask, you can prepare your web page in a way that is compatible with decorators.

In real-life applications, you may not often see decorators achieved with most code builds. Especially in popular Python frameworks, it is sufficient to just add ready-made decorators to your projects.

However, in this post, we would like to discuss what is the basis behind decorators.

image created by the author

3. What are the advantages?

We can list the advantages as follows:

Avoiding designing complex code blocks

Creating faster workspaces with less memory

Facilitating the use of automatic functions related to the frontend in a certain order

Using new code quickly within a function

The decorator with the ‘@’ sign is used in the top line of the function and can be disabled (converted to the comment line) if it is desired not to affect the subfunction. So it can be controlled depending on how much and how you want to work with the original function.

Then it’s easy to delete this in just one decorator line if you don’t want that extra functionality.

4. How can I understand better?

Check the examples below to better understand its usage.

First, let’s try to understand how it can be used by defining a simple function by assigning it to another variable:

When we call the function, we get the result as follows:

When we assign this function to another variable, we again get the same result:

Now let’s delete the main function first:

And then let’s call x again:

Even though the original function has been deleted, x is still running. This is how the Python function allows assigning to another variable.

Well now let’s go ahead and try to understand it with another example.

Let’s examine the code blocks that have other functions. For example:

As you can see, Python did not recognize the sub-function in the result section. We’ll use print to achieve this:

We have also shown the indented function. Now let’s add another function.

When we call the welcome () alone, which we defined in hello (), we actually get an error for it:

So we can make below to call the functions within the ‘hello’ function separately:

Or if we call it with the default name in the function, Kurt to give the other option:

As you can see, we were able to view only sub-functions. This; is the idea of returning another function within a function.

Let’s give another example:

Now let’s define a new function as if we needed a decorator. I’ll call this func_needs_decorator:

Now let’s combine the two with a variable:

Now let’s see if we can get the same result thanks to the new_decorator we created. For this, we will make use of the special syntax used in the display of decorators, namely → @.

If you wish, you can get rid of it thanks to the comment line with the open-close logic. Then it will look the same as before:

Essentially, what decorators do here is surrounding.

5. What are the examples of its applications?

As we briefly mentioned above, let’s discuss some of its applications for Flask.

For login requirement: It facilitates the structure of the code that queries the real person’s fulfillment of the opening instruction for a login process. For example, we can see this while repeating the password and authentication section before logging in when the required condition is not met.

For ideally cache: Let us assume that an initialized cache object exists. If you are also curious about creating a cache, you can get information from here. When you want to keep the cache usage limited, decorators can be adapted to what they want to do here.

After generating the cached key, it calls the desired time and memory usage according to this key. Then the original function enters the environment after the decorator for the other state. Thus, the decorator provides the advantages of using limited memory.

For auto template: In addition, template maker decorators are also widely used today. Basically, it allows the values obtained from the view function to be returned in dictionary format. In this use, decorators act as automatic formers.

For flexible endpoint: When you need a distribution dispatcher, it appears when you want to adopt the ‘werkzeug.router’ system to more flexible work. In the distribution of the URL address with ‘Rule’, the endpoint decorator is included in the parenthesis by synchronizing the index.