[Music] welcome back aliens my name is Devin Wendy and in this video we'll talk about one of the amazing feature of Python which is decorators nothing about this when you talk about functions functions are built to perform certain tasks right so example let's say we have this file and in this file we have a function which is predefined so let's get a function here and this will be for division of course you can write any complex code but just to keep it simple let's go for division and of course a division function takes two parameters so we'll take a and B and this will return the division of this two so we'll say return a divided by B simple code right of course you can make it complex you can go for complex code by just to keep it simple the got two lines a division which divides two numbers and then if you call this function if you say let's see if I pass for my two and if you run this code of course you have to also print and short frittering here let's print so if you call this function now let's right-click and say run you can see we got two points so this is what we wanted right we wanted the output okay that's great but what if if I pass the value which is two and four so what are you expecting the output here so the output which I'm expecting here is of course 0.5 because that's what you will get and you can see we call 0.5 so what if I say I want a different logic here the logic which I want is doesn't matter in which sequence I pass the value it should be always the numerator should be bigger than the denominator so example in this case if I'm passing 2 and 4 it should be reverse while dividing only when my numerator is less than denominator I want to swap them ok so in this case even if you are passing 2 and footage the dividend should be 4 divided by 2 okay so we can do that right we can have our own logic so just for the example we are going for this ok so what do you think what you do so of course as a user I should be passing 4 and 2 but let's say as a user I want to pass 2 and 4 it is your job developer to make sure that they are getting swept okay you will say ok it's my job I will do it so you will go to the existing code with the division and you say okay so before dividing those two numbers I want to apply a logic the logic is if a is less than B I just want to swap them and we know how to swap two numbers right we can simply say a comma B is equal to B comma a simple logic and then once you have done that if you're on this code you can see we called 2.0 it was so simple right but we got a twist the twist is as I mentioned imagine this code the division code which you have here is not with you this is in some other file and you're importing it maybe you don't have the access for this function and maybe you don't want to change the code of the existing function so I want you to swap those two values without touching the new function is it possible and that's where decorators comes into picture so what the decorators so using decorators you can add the extra features in the existing functions I know that sounds weird but we can do that so just to explain that whatever you do is either create a new function now this will be a decorator for teams I will say we're quite a smart because it can change the code right so it's a smart Dave now what this smart do will get now smart Dave will do as the function or the parameter so let's pass the function here so it will accept a function again while we talk about that later so this mod deal will accept a function and then so if you want to change the logic you have to write a code right I want to do that in another function so we can write a function inside a function that's the beauty of Python so let's create a function here so we'll say def we can have any function name let's go for dinner because they're in a function and then this inner function will take the same parameter which is taken by Dave two parameters let's take a comma B the name should not be same but you can have any name but the number of parameter should be same so you are passing - you have to accept - now in this inner you can write the logic which you are trying to write in dev so which was if a is greater than P in this case you will swap them so swapping is a comma B is equal to B comma a just to remove these spaces there let's do that and that's okay which have a space here so you can see we are creating a smart function here which has enough function who is doing our job so this is the code which I want inside my dev now once you have done that you simply have to return so you have to return the function which you are accepting here basically you know you are actually calling Dave here so in the return you will pass that two values a and B the new values so the original values were 2 & 4 after swapping you're passing food & 2 so you are calling a function by passing these two values now this function is your D function but you'll be thinking how they are matching just give me some time and then after this you just have to return the inner function because that's the function which is actually doing the job for you okay but will this work let's try let's run this code and you can see you're still getting 0.5 is because there is no connection between this mod Dave and this Dave let's create that connection so what we can do is before calling the duty function we can say Dave is equal to let's make it smart so we can assign the function to a function because everything in Python is no object right so against the DV is equal to smart Dave in which you will passing the new function now this new function is the original function you have and this is a new one in fact you know just to give you an idea I'll make this as do one and let me call that function do one by passing two and four so basically we are calling div but indirectly okay so we are calling de one which is calling which is using this mod do function by passing the values it will swap the values in build and then it will actually call the do function at the end which will print the values let's run this code and you can see regard the output which is wrong that's weird okay I guess there is something wrong with the if condition my bad so if errors okay so as a programmer you always make mistakes right so you can see it should be less than right that's what you want to smell so if you have this code now or we got to that's what we wanted right in fact the amazing thing is you don't have to go for a new name you can actually replace with the original itself which is Dave Dave so it looks like we are calling this new function but no just before calling them we are changing the definition for D right so this is the old Dave we are making a new deal now so you can see record 2.0 the amazing part about decorators is you can change the behavior of the existing function at the compile time itself so that's the decorators for you so just to recap what we are doing is we are creating a new function which takes function as a parameter and that's the beauty that this is not possible in all the languages so yes we can do that in Python because python is also a functional programming and then we can define a function inside a function which is actually replacing the code of div behind the scene and then we before pausing deep we are saying Dave is equal to smart Dave and we are passing Dave so basically we are changing the way they works so that's how you can use decorators in Python I hope you got something about decorators in this video so I hope you are enjoying this series let me in the comment section and do subscribe for further videos