This Decorators in Python-Complete Tutorial is made for you. At the end of this tutorial, you will have full knowledge of the Decorators in Python.
Hello, & Welcome!
Part 18- Decorators in Python-Complete Tutorial
In this article of Python Tutorial, you will learn following-
- What is a Decorator in Python?
- How to Create a Decorator in Python?
- Use of Decorator in Python.
What is a Decorator in Python?
A Decorator extends the behavior of the function. A decorator allows implementing new features to an existing function without modifying the function itself. You can call the decorator before the function in which you wanna decorate.
For example-
@class_method
def commons(cls):
Here, @class_method is a decorator. It extends the behavior of the function and it describes the function behavior.
How to Create a Decorator in Python?
A Decorator is itself a function. You can create your own decorator, so for creating own decorator, let’s see in the example below-
Before defining any decorator, use the “@” operator.
Syntax of Decorator-
@decorator_name
def function_name
#function body
Call decorator before any function, in which you wanna use a decorator.
Example-
def cough_dec(func):
def func_wrapper():
print("cough") #code before function
func()
print("cough") #code after function
return func_wrapper
@cough_dec
def question():
print("can you give discount?")
@cough_dec
def answer():
print("it's only 50")
question()
answer()
OUTPUT-
*cough*
can you give discount?
*cough*
*cough*
it's only 50
*cough*
Here, “def func_wrapper()” is a wrapper function, which is defining the decorator. “return func_wrapper” is returning wrapper function. “@cough_dec” is a decorator.
To create your own decorator, you can create it as a function like in the example “def cough_dec(func)“. And inside that function, create a wrapper function like “def func_wrapper()”. Where you can define the behavior of a function.
After that, outside from wrapper function “def func_wrapper()”, return it as “return func_wrapper”. So, by doing this you can create your own decorator and then you can use this decorator many time, whenever you want.
Use of Decorator in Python–
Decorators are used in –
- Logging,
- Run time check,
- Synchronization,
- Type checking,
- Debugging,
- In web framework and many more.
That’s all for the Decorator in Python. I hope now you have a better understanding of the Decorator in Python.
Congratulations! You successfully learned the Decorator in Python.
In the next tutorial, we will start learning Reading & Writing files in Python.
Till then, Enjoy Learning Python!
Are you looking for Best Books on Python for Beginners and for Intermediate/Experts?
Check these Books, These Books are the best selling and covered everything in detail.
I hope you will find these Books helpful-
Best Selling Python Books for Beginners
Best Selling Python Books for Intermediates/Experts
All the Best!
Thank YOU!
Though of the Day…
‘“Live as if you were to die tomorrow. Learn as if you were to live forever.”
– Mahatma Gandhi
Written By Aqsa Zafar
Founder of MLTUT, Machine Learning Ph.D. scholar at Dayananda Sagar University. Research on social media depression detection. Create tutorials on ML and data science for diverse applications. Passionate about sharing knowledge through website and social media.