Understanding Decorators

Akansha Srivastava
2 min readMar 1, 2021

'''Decorators are the functions that add some functionalities to a given function. they don't change the function/structure of the function but just add a few lines of code below and after the function .

A
wrapper is a function that provides a wrap-around another function. While using decorator ,all the code which is executed before our function that we passed as a parameter and also the code after it is executed belongs to the wrapper function. The purpose of the wrapper function is to assist us.
'''


def dec1(func): # this is a decorator
def executor(): # this is a wrapper function
print("before execution")
func()
print("After execution")

return executor


'''there are two ways to call a decorator ,
1. we can assign the value of the decorator to the original function
syntax: func1=dec1(func1)
2. we can use @ sign before the declaration of the new function .
'''


@dec1 # this is a symbol for the decorator ,it is equal to func1=dec1(func1)
def func1():
print("this is the original function ")


# func1 =dec1(func1)
func1()

'''Advantages of decorators :
1.they help us to wrap different functions around some code we want to be executed before and after the function
2. multiple decorators can be used with a single function
3. We can use decorators in authorization in Python frameworks such as measuring execution time.'''

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

OUTPUT

before execution
this is the original function
After execution

--

--