Decorators in Python

In this topic video, I don’t understand the complete meaning and create confusion for me.

Could you explain… what is the use of decorators with one simple program and share pdf of this topic if possible?

Decorators in python allows us to modify the behavior of an already existing function or class. Decorators allow us to wrap another function in order to extend the behavior of wrapped function, without permanently modifying it.
You can also say that decorator add additional features to an already existing function without changing the function itself .
In decorators, functions are taken as argument into another function .
Eg-

@decorator
def functions(arg):
    return "value"

This is equivalent to -

def function(arg):
    return "value"
function = decorator(function) # this passes the function to the decorator, and reassigns it to the functions

Decorators are used to provide make your functions more flexible. By using them, a lot of redundancy is avoided. As I hope in video you have seen that initially we required the add function that takes 4 arguments but later on by using decorators we just passed only the function arguments. It makes our task easy.
In short I just want to say,

  1. Decorators are a way to add functionality to your function without writing the same thing again and again
  2. It makes your work easy and saves memory
  3. As sometimes it’s like you can save a lot of files and memory as you don’t have to write that same redundant functions again and again
    take it like you are calling a function within a function
    I hope it is clear to you what is the use of decorators
    Incase there is still any confusion pls let me know I will help you out and incase it is clear to you pls mark it as resolve and provide the feedback so that we can improve our services.
    Thanks :slight_smile:
    Happy Coding !!

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

@singh.manpreet1308 can you pls explain your doubt more clearly what do you want to know. May be our previous explanation isn’t clear to you. Please ask if anything you doesn’t understand. I will surely help you out.

Doubt clear thnx…

It’s good to hear. You can post more doubts and ask anything related to your course. We will surely help you out :slight_smile:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.