Hey there, this is your facilitator commander for this course decorators in Python. In this video we'll be discussing chaining decorators in Python we had already discussed the decorators importance, the syntax the workflow few examples, in this will be going further deep and we'll be discussing how to chain multiple decorators. This concept is very easy and very important also, a decorator can be created another decorator can be created and both those these decorators can be chained together another function for example, the function that needs to be decorated. So for example, if we are looking to decorate maybe for example a meeting, we can create multiple decorators to make that greeting work. Let's discuss this in the form of example. I'm creating a hash a decorator that takes in arguments and key value arguments and prints hash 50 times before the execution of the function to be decorated and after the function execution of the function to be decorated, the syntax remains the same.
Inside the decorator function, there is another function and then we return that function. And in that function we take, we are actually getting the function to be decorated. And before it we are printing hash which attempts and after a few times, and let's execute this. Okay, executed. Now we're creating another decorator function that does exactly the same thing. But instead of hash it prints dash.
So now we have defined two decorator functions. One is the hash, hash a and the other one is the dash. I'm not going deep into the sun. text of the decorator because we had already discussed. But in case if you needed just a brief idea, this is your first video. In decorator we take in the function as an argument and return another function with some additional functionalities.
So here like you can see, we are returning the function taken as argument. And before it we are printing 50 times and after it does 50 times. Okay, so let's execute this another function created show it prints the greeting that's being given as the argument. And we're decorating it twice, once with the Hashi declaration and then as the dash deck. So let's execute this and move forward. And now I'm passing Hello into the greeting function into the show function that I created.
And when I just run it, it's going to print hello with hash and dashes 50 times each Now this decorated or this chaining of decorators to decorators with this deck would be like many decorators that chained together. And it works like the first decorator the second decorator, the third decorator, the fourth decorator so the sequence matters. And in other words we can create the chaining of decorators like this create another variable show again and we passed in hasher. Inside that this we passed the another decorator that we need this to be decorated with and then the last final function that needs to be backwards. This is exactly the same as this, they just told you that the order of the decorator matters. So if we just reverse the order, we pass dash first and then hashey and we decorated show with these two and then once again, we execute This with the Hello there.
So it's going to print the mesh first and then hash first chaining of decorators is similar to multiple inheritance where in multiple inheritance the function takes in attributes or gets inherited attributes from multiple parent classes. So here, it can get different function can get multiple in statements from multiple decorators. So and how we do it, we just include them one by one in declaration line with declaration syntax and the function to be decorated. I hope you have a clear understanding of chaining decorators. The concept is very simple. It's similar to multiple inheritance, where in multiple inheritance, the child class gets attributes from multiple parent classes and here The function to be decorated can get multiple functionalities from multiple decorators.
And the way it gets it depends on the order in which the function is decorated. May you have any queries feel free to contact me, you can contact me by email by Twitter by on Udemy. And if you're looking to get connected, you can find my blogs on medium, you can email me and again, you can contact me on Udemy and as well as Twitter, and also subscribe to my youtube youtube channel. And the upcoming video we'll be discussing debugging decorators. So this is related to debugging of decorators. We'll discuss it and hopefully you will have a very good understanding of all the concepts related to decorators.
Thank you