Answer the question
In order to leave comments, you need to log in
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
In general, I'm trying to learn python and I can't understand the essence of decorators.
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):
....
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
def my_loger(func): # Объявляем декоратор
def new_func(*args, **kwarts): # Объявляем функцию, которая заменит исходную
print("Begin send") # делаем что то важное
func(*args, **kwargs) # Вызываем исходную функцию
print("End send") # Опять делаем что то важное
return new_func # Возвращаем новую функцию
@my_loger
def send_mail(test):
pass
def send_mail(text)
print("Begin send")
# Some actions
print("End send")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question