Questions #1


What can be a decorator? What can a decorator be applied to?



Download 85,32 Kb.
bet7/24
Sana06.07.2022
Hajmi85,32 Kb.
#752107
1   2   3   4   5   6   7   8   9   10   ...   24
Bog'liq
interview en

What can be a decorator? What can a decorator be applied to?
A decorator can be any callable object: a function, a lambda, a class, an instance of a class. In the latter case, define a __call__ method.
You can apply a decorator to any object. Most often to functions, methods and classes. Decorating is so common that a special @ operator is dedicated to it.
def auth_only (view):
...


@auth_only _
def dashboard (request):
...

If the decor operator didn't exist, we would write the code above like this:


def auth_only (view):
...


def dashboard (request):
...

dashboard = auth_only(dashboard)



What happens if the decorator doesn't return anything?
If there is no return statement in the function body, the call will return None. Remember, the result of a decorator replaces the object being decorated. In our case, the decorator will return None and the function we are decorating will also become None. When trying to call it after decoration, we get the error NoneType is not callable.
What is the difference between @foobar and @foobar()?
The first is the usual decoration with the foobar function.
The second case is decorating with the function that foobar will return. In another way, this is called a parametric decorator or decorator factory. See next question.
What is a decorator factory?
This is a function that returns a decorator. Such a decorator is rarely placed in a separate variable. Instead, decorate with the result of calling the decorator factory.
For example, you need a decorator to check permissions. The validation logic is the same, but there can be many rights. In order not to produce copy-paste, we will write a factory of decorators.
from functools import wraps


def has_perm (perm):
def decorator (view):

Download 85,32 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   10   ...   24




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish