E
E
Eugene Moldovanu2020-06-01 07:21:34
Python
Eugene Moldovanu, 2020-06-01 07:21:34

Is it possible to work in python without understanding the essence of decorators?

In general, I'm trying to learn python and I can't understand the essence of decorators. I don’t even understand where they can be used, I wrote a lot on object pascal and have never come across anything like this, although I used OOP.
The question is - is it really possible to write in python without using decorators? Initially, it seemed to me that yes, but then I saw messages that everything in Dzhang and Flask was built on them. It's true?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey Pankov, 2020-06-01
@trapwalker

In general, I'm trying to learn python and I can't understand the essence of decorators.

It all depends on how long you've been trying to understand them and still can't.
If after thinking for a couple of minutes you are already desperate, then probably programming is not your path.
If, after reading a couple of articles, you could not figure it out, then maybe you should make another attempt. Or do you think that this is straight rocket science and since you didn’t master it from the first approach, then it’s for life now to “work without decorators”. Fate.
In general, I am surprised by such fatalism. Well, you don’t understand something, so figure it out, and don’t run to find out whether you can live your whole life without it. Decorators are a very simple concept.
If you are not able to understand it, then you will not be able to program professionally - this is a fact.
And no, the light did not converge on the decorators. You need to understand that in python, a function is an object of the first kind and what this means.
You need to understand what a "closure" is in the context of functional programming.
Moreover, you need to understand what descriptors are, how meta-classes work, and much, much more.
No, they are not born with this knowledge, they need to be mastered.
Decorators are a very simple concept.
A decorator is a function that takes a function as its only argument. The decorator does something with this function (registers it somewhere, documents it, wraps its call into another function) and returns its modified or another wrapper function.
What specifically do you not understand?
It should be noted that the Decorator as a design pattern has a wider meaning. You can decorate objects, classes, functions, even modules (insofar as they are also objects). You need to study, sir, and not expect everything to be sewn into the brain instantly. Being a programmer is always about learning. Do not stop.
There are a lot of examples of decorators: the simplest and most understandable are:
- measuring the execution time of a function
- caching the result
- substitution of part of the arguments ( partial )

S
Sergey Gornostaev, 2020-06-01
@sergey-gornostaev

Decorators Python
WHY do you need decorators?

D
Dr. Bacon, 2020-06-01
@bacon

What does understanding mean? You can understand what a particular decorator does, but not understand how it works, that is, it is a "black box" for you. Or do you not even understand the meaning of such structures?

@login_required
def my_view(request):
    ....

Threat will have to be used, especially in flask, but you can not create it yourself.

Y
Yermek, 2020-06-01
@ermek6

I'm not getting into Python experts, I'll just say how I understood them:
Let's say we have some function that sends a file by mail

def send_mail(text):
    pass

And suddenly, we wanted the sending to be logged somehow in a special way, and we want to connect the logging function to our other functions, of which there are so many that it’s hard to write the same thing everywhere (that is, not only when sending letters, but also when saving to disk is not an example). To do this, we write a function where the @ decorator will automatically substitute our function. Our function must also return a function.
def my_loger(func):    # Объявляем декоратор
    def new_func(*args, **kwarts): # Объявляем функцию, которая заменит исходную
        print("Begin send")   # делаем что то важное
        func(*args, **kwargs)    # Вызываем исходную функцию
        print("End send")    # Опять делаем что то важное
    return new_func   # Возвращаем новую функцию

Now, when declaring functions, we can write @my_loger in front of it, and logging will be connected automatically.
i.e. by writing the following
@my_loger
def send_mail(test):
    pass

actually we will get the code
def send_mail(text)
    print("Begin send")
    # Some actions
    print("End send")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question