By definition, a decorator is a function that takes another function and
extends(/decorates) the behaviour of the latter function without explicitly modifying it.
Let see how we can apply decorators for Functions or Methods
Level I:
We might heard the term, 'sending a function as a parameter to another function'
Practical Example:
def firstFun():
print ("I am the firstFun")
def secondFun(inp): # we are receiving a function(inp=furstFun) as a parameter
inp()
print (" I am the secondFun")
secondFun(firstFun)
Output:
I am the firstFun
I am the secondFun
Observation from the output:
We sent one function(firstFun) to another function(secondFun) as a parameter, and
it helps to utilize that function(firstFun) according to the need.
Level 2:
Now we are going to see the same functionality but pythonic way(Decorator)
def secondFun(inp):
inp()
print (" I am the secondFun")
@secondFun
def firstFun():
print ("I am the firstFun")
Output:
I am the firstFun
I am the secondFun
*** That's it, Now you have done with what & how about Decorators in python***
Now, Again to the definition of the decorator,
By definition, a decorator is a function that takes another function and
extends the behaviour of the latter function without explicitly modifying it.
From our example program,
we are sending firstFun to secondFun and according to the need, we can add more behaviour along with
the original function(firstFun) without modifying it(firstFun)