A decorator is the function which takes another function as an argument to change its result or to give it some effect.
For example, with the code below:
# 4 + 6 = 10
def sum(num1, num2):
return num1 + num2
result = sum(4, 6)
print(result)
We can get the result below:
10
Next, we created minus_2()
to subtract 2 from the result of sum()
as shown below:
# (4 + 6) - 2 = 8
def minus_2(func): # Here
def core(*args, **kwargs):
result = func(*args, **kwargs)
return result - 2
return core
def sum(num1, num2):
return num1 + num2
f1 = minus_2(sum)
result = f1(4, 6)
print(result)
In short:
# ...
result = minus_2(sum)(4, 6)
print(result)
Then, we can get the result below:
8
Now, we can use minus_2()
as a decorator with sum()
as shown below:
# (4 + 6) - 2 = 8
def minus_2(func):
def core(*args, **kwargs):
result = func(*args, **kwargs)
return result - 2
return core
@minus_2 # Here
def sum(num1, num2):
return num1 + num2
result = sum(4, 6)
print(result)
Then, we can get the same result below:
8
Next, we created times_10()
to multiply the result of minus_2()
by 10 as shown below:
# ((4 + 6) - 2) x 10 = 80
def minus_2(func):
def core(*args, **kwargs):
result = func(*args, **kwargs)
return result - 2
return core
def times_10(func): # Here
def core(*args, **kwargs):
result = func(*args, **kwargs)
return result * 10
return core
def sum(num1, num2):
return num1 + num2
f1 = minus_2(sum)
f2 = times_10(f1)
result = f2(4, 6)
print(result)
In short:
# ...
result = times_10(minus_2(sum))(4, 6)
print(result)
Then, we can get the result below:
80
Now, we can use times_10()
as a decorator with sum()
above @minus_2
as shown below:
# ((4 + 6) - 2) x 10 = 80
def minus_2(func):
def core(*args, **kwargs):
result = func(*args, **kwargs)
return result - 2
return core
def times_10(func):
def core(*args, **kwargs):
result = func(*args, **kwargs)
return result * 10
return core
@times_10 # Here
@minus_2
def sum(num1, num2):
return num1 + num2
result = sum(4, 6)
print(result)
Then, we can get the same result below:
80
Don't forget that if a function has multiple decorators as above, they are executed from the bottom to the top as shown below:
# ((4 + 6) - 2) x 10 = 80
@times_10 # 2nd executed.
@minus_2 # 1st executed.
def sum(num1, num2):
return num1 + num2
Then, we can get the same result below as we've already seen it in the above example:
80
So, if we change the order of them as shown below:
# ((4 + 6) * 10) - 2 = 98
@minus_2 # 2nd executed.
@times_10 # 1st executed.
def sum(num1, num2):
return num1 + num2
Then, we can get the different result below:
98
Lastly, we created the code below in Django to run test()
in transaction by @tran
:
# "views.py"
from django.db import transaction
from django.http import HttpResponse
def tran(func): # Here
def core(request, *args, **kwargs):
with transaction.atomic():
return func(request, *args, **kwargs)
return core
@tran # Here
def test(request):
person = Person.objects.all()
print(person)
return HttpResponse("Test")
@discombobulated
if you wanted to. Decorators are just functions. – Transmutation