03 Python Decorator Details

7 minutes
Share the link to this page
Copied
  Completed
An understanding of decorators definition, details and how it works

Transcript

Hello, welcome back. This is your facilitator commander for this course decorators in Python. decorators belong probably to the most beautiful and most powerful design possibilities in Python. But at the same time, the concept is considered by many as complicated to get into to be precise, the usage of decorators is very easy, but writing decorators can be complicated. However, I will make it simple for you. In simple terms, decorators are the function class methods that adds or modifies functionality of an existing function class or method without actually changing the actual class method or function.

So what it does it change or add the functionality of an existing class function or method and how it does it does not actually change it but adds to it or modifies to it through another function. And that function is part of the decorator function. I'll show you how, in simpler terms. Python decorator are the function that receives a function as an argument and returns another function as return value. So, it is taking a function as an argument. And this is the function that needs to be decorated or something needs to be added to that function.

And this function is taken as an argument and returns another function as a return value and that another function that has been returned as an as a return value has the actual function that is being taken as the argument and some more added functionalities to it. assumptions of a decorator that will pass a function as an argument. So this function that decorator function is going to take another function as an argument, and the signature of inner function in the decorator must match the function to decorate So, if the function that is being taken an argument has some thing as an argument also required, so, that arguments should also be required in this decorator function. This is what we mean by saying that the signature of inner function in the decorator must match the function to decorate. Taking this in the form of example, I have just created a decorator function that takes another function as an argument.

And meant this function is created when this decorator function is created, another function is created and this function has the functionality of print. And then this argument function is being called here and at the end of this decorator function, the whole this function is returned. That means This is going to be printed and this whatever is in this function is also going to be performed or executed. So actually what has happened, this function has got an additional functionality of printing. And that is what the decorators does. And I've tried to explain this in very simple and very clear language and step by step.

Let's move forward. Now this is just the the decorator function we discussed. isn't a normal function. It print I am ordinary. And when we run this normal function exactly does this Okay, now the decorator function that we just created earlier. Okay.

And then I'm adding a decorator to this normal function and this is the way of adding decorator by accurate and the name of the function that needs to be added. So, what it is actually going to do, it is going to this decorator function is going to take this for function as an argument and then it is going to return this function to be added. Now, when this function is called, it has the functionality of printing I got decorated and then it is going to call this function that has been passed as an argument which is this and it will have I am ordinary function so, when I get on this It will what it will does it will not only print this but this also before printing this. So, I got decorated I am ordinary, you see. So, this function got decorated and it got additional function functionality of the decorator function and this is the core of the decorators.

There is another way of decorating this function and I'll, I'll show you how. Okay this is another way of writing a decorator function where we have passed the actual function as an argument in the decorator function and when we run this, it has printed I got decorated I got decorated and I am ordinay. It is printed Got decorated twice. And you know why? Because this function has already been decorated under the decorator function. And we just re decorated it and therefore it got decorated twice.

And it printed this thing twice. So I hope in this video you've got an intro of the decorator function, and a simple example about how to create the decorator function and how to implement it. And the upcoming videos we're going to go more deep into this decorator functions. I hope you've got a better understanding of decorators in Python and this flow of the way the topic is discussed is pretty simple and easily understandable. Don't forget to keep in touch with me. And for that you can subscribe to my channel.

You can get in touch with me on Twitter, on YouTube, or you can just message me on my email. Thank you

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.