In this tutorial, you will learn how you can create a decorator and why you should use it. Before you understand decorators, make sure you know about how Python functions work. If you don’t know about python function, then check our Functions And Docstrings tutorial, because functions are the fundamental concept in understanding python decorators. Functions in Python can be defined as lines of codes that are built to create a specific task and can be used again and again in a program when called.
Figure1: Functions in Python
Decorator as can be noticed by the name is like a designer that helps to modify a function. The decorator can be said as a modification to the external layer of function, as it does not make any change in its structure. What a decorator does is, it takes a function and insert some new functionality in it without changing the function itself. A reference to a function is passed to a decorator and the decorator returns a modified function. The modified functions usually contain calls to the original function. This is also known as metaprogramming because a part of the program tries to modify and add functionality into another part of the program at compile time. Understanding the definition could be difficult but we can grasp the concept easily through the example in the video section. In terms of python, the other function is also called a wrapper.
A wrapper is a function that provides a wrap-around another function. While using decorator all the code that 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. Like if we are dealing with a number of similar statements, the wrapper can provide us with some code that all the functions have in common and we can use a decorator to call our function along with wrapper. A function can be decorated many times.
Note that a decorator is called before defining a function.
There are two ways to write a Python decorator:
def inner1(func): def inner2(): print("Before function execution"); func() print("After function execution") return inner2 @inner1 def function_to_be_used(): print("This is inside the function") function_to_be_used()
• Decorator function can make our work compact because we can pass all the functions to a decorator that requires the same sort of code that the wrapper provides.
• We can get our work done, without any alteration in the original code of our function.
• We can apply multiple decorators to a single function.
• We can use decorators in authorization in Python frameworks such as Flask and Django, Logging and measuring execution time.
We can do a lot with decorators like Multiple decorators that can be applied to a single function. I hope this tutorial serves as a good introduction to decorators in Python. After understanding the basics of Python decorator, learn more advanced use cases of decorators, and how to apply them to classes.
# def function1(): # print("Subscribe now") # # func2 = function1 # del function1 # func2() # def funcret(num): # if num==0: # return print # if num==1: # return sum # # a = funcret(1) # print(a) # def executor(func): # func("this") # # # executor(print) def dec1(func1): def nowexec(): print("Executing now") func1() print("Executed") return nowexec @dec1 def who_is_harry(): print("Harry is a good boy") # who_is_harry = dec1(who_is_harry) who_is_harry()
No downloadable resources for this video. If you think you need anything, please post it in the QnA!
Any Course related announcements will be posted here