📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

Уроки Python с нуля / #20 – Декораторы функций

Школа itProger / Программирование8:01

Transcription

Hello! In this lesson, we will learn about decorators in Python. Before we begin, I would like to recommend the website itprager.com. On this site, you will find homework assignments and a lot of other useful information. The link to this lesson will be in the description of this video.

Before we dive into specific decorators, let's implement a simple function that takes one parameter: a URL of a specific website. We will create functionality that allows our program to open this website in your default browser.

To implement this program, we need to use a built-in module called `webbrowser`. So, at the top, we will import this module. We import `webbrowser`.

Next, let's create a function. We can name this function `open_url`. The name can be anything; I chose this name. It will take one parameter, which is the URL address that we will use to open the website. In this function, we will simply call the `open` function from the `webbrowser` module, passing the URL address to it.

In my case, this will be the URL, and essentially, when we call this function and pass a URL address, the desired website will open in the default browser.

Let's test this now. I will call this function and pass a specific site. For example, I will specify `https://itprager.com`. If I run the program now, we will see that, as you can see, the site itprager.com opened in my default browser.

In the same way, you can open any other websites using this module, which is quite convenient.

Now, let's implement the concept of decorators. With decorators, we can execute some code before and after a specific function is executed.

To start, let's create a simple decorator that will just print information before and after executing a piece of code. Later, we will add some logic and functionality to it.

To create a decorator, we first need to create nested functions. We can call our function `validator`. For now, this name will be somewhat meaningless, but later we will actually use it to validate the URL address. If it is correct, we will open the page; if it is incorrect, we will show an error to the user.

So, when we create a decorator, we always name our initial function whatever we want. I named it `validator`. Here, we need to accept one parameter, usually called `func`, which is a function. I will use this name.

Next, inside this function, we create another nested function, usually called `wrapper`. So, I will name it `wrapper`. In English, this means "wrapper."

Here, we will specify some text. For example, we can write "This is text before the function." Then, we will execute the function that is passed as a parameter, and we will also write some text after the function. We can say, "This is text after the function."

At the end, we need to return the `wrapper` function from our main `validator` function. So, I will just return `wrapper`. Do not add parentheses at the end. If you add parentheses, it will execute the function immediately, which we do not need. We just need to return it.

Now, I can decorate our `open_url` function using the decorator we just defined. To do this, I will use the `@` symbol followed by the name of our decorator, which is `validator`.

At this point, I will encounter an error because this function is supposed to accept one parameter, but I am not passing any parameters here. So, let's modify it to accept a parameter, which can be named anything. We will also pass this parameter to our function.

Now, if I run the program, everything will work correctly. The desired page will open, and we will see the text before and after the function execution.

You might wonder why we need this. This can be useful for performing checks before executing the function itself.

Let's write a check for the validity of the URL address. I will check if there is a dot in the URL string. If a dot exists, I will assume that the user provided a valid URL address, and we will open the page in the browser. If there is no dot, we will inform the user that they entered an invalid URL.

Now, I will remove the dot from the URL I pass. In this case, we will see that our `open_url` function does not have any additional checks. As you can see, it outputs "Invalid URL" because I passed an invalid URL. If I pass a valid URL, the function will execute, and the desired page will open in the default browser.

Thus, with decorators, you can easily add additional functionality to any function, which can execute before or after your function.

This is convenient because I can create 200 additional functions without needing to add similar checks to each one. Instead, I can just add the `validator` decorator to all of them, and the necessary checks will be included in all those functions.

This is quite convenient; it reduces code and allows you to add additional actions when executing your desired function.

Moreover, if you ever want to remove the checks from a function, you can simply remove the decorator, and the function will no longer have any checks. If you want the checks back, just add the decorator again, and now your function will have the decorators and checks.

In this way, you can create multiple decorators, each performing its own functionality. If you need to add this functionality to a function, you simply add another decorator, and everything will work correctly.

Such decorators are not very commonly used in the development of various websites, especially with frameworks like Django. They are also not frequently used when working with libraries like Kivy, which allows us to develop applications for Android.

However, you may encounter decorators often in the future, so it's worth knowing and understanding how they work.

Well, that concludes our lesson. I hope you enjoyed it. If so, don't forget to subscribe to the channel and join our social media. All the links will be in the description of this video.

That's all from me. See you next time! Goodbye!