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