Advance Python-Decorators Indepth Implementation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello all my name is krishnak and welcome to my youtube channel so guys we are going to continue the python advanced python series and in this particular video we are going to discuss about decorators if you have heard about some of the frameworks like flask and django we are already going to use decorators over there and probably in just a couple of days i'm going to start that playlist of flask and xango and there is also one more playlist that is going to come up that is basically cloud computing playlist which i'm going to start with respect to aws so when i'll be starting flask and django there also will be using decorators but just let us understand what exactly decorators is and how do we actually use it and when to use it now remember one thing over here guys before understanding decorators will go in this particular order first is how to do a function copy the second is we need to understand closures and the third final thing is we are going to understand about decorators so let me just take a very good example and let me just create a function and here probably i'm just going to write a printer and i'll say welcome to fresh nike youtube channel suppose uh i'm just going to write it over here okay and then i am just going to execute it after executing this if i really call the welcome message and then i'll basically get the output now just see this if i try to the first point that i'm talking about is function copy if i try to use a variable saying as welcome or let me make a variable as wel and suppose if i execute it now if i go and see this well what it is it is not giving me anything as such and but remember one thing if i also try to use this as my function or whether it will say non-type object is not callable this is fine let me remove this now suppose instead of writing welcome what i'm going to do is that i'm just going to return something okay i'm just going to return something over here so here i return it and let me just execute it again and now if i go and see well now here you will be able to see that i'm getting that specific message now i'll do one specific one simple change is that after assigning this well is equal to welcome because welcome is my function i'm just going to delete this welcome over here okay i'm just going to delete this welcome over here so welcome is my function name now just tell me whether this well value will again be having this or it will not be having this you know so this is the first thing that i really want to ask you all just consider this as a interview question for all of you whether this function will get copied or whether it will get deleted what will happen if you get the well value just try to comment it anyhow i'll try to show you the output over here now let me just execute it once again and now if i go and see my well again we are getting welcome to krishna youtube channel so that basically means we have just copied this function into my another variable that is well and this is basically giving me the output of this okay now this is the most important thing this is called as a function copy okay we have basically copied the function over here to here now if i try to again call my welcome function it will be giving me an error saying that welcome is not defined okay so this particular function is not defined okay now this is very very important to understand this is the idea about function copy now the second point that i'm going to discuss about is basically called as closures now what is closures usually initially when i was working as a dot net developer we used to write functions inside a function okay so when we whenever we write functions inside a function it is basically called as a closures function and even in javascript i think you have read about it suppose let me just say that i am going to uh do the main welcome over here and inside this i am passing a message saying that okay and this message can be anything inside this i'm just going to call another sub welcome class okay another sub welcome class and probably this sub welcome class is going to print something like this welcome to krishna youtube channel and remember guys you need to know all these things because that will actually help you to understand about decorators in a better way so i'll say welcome to krishnak youtube channel and then i will print this specific message now again the question is that when i am providing this parameter as message or let me just remove this and let me just define this message over here saying as hi hi everyone hello everyone suppose hello everyone okay if i'm defining like this okay and i'm printing the message over here and i'm saying that print please subscribe subscribe fresh channel okay which i think you will subscribe so please do subscribe it uh please subscribe fresh channel okay so fine this is my message this is my method that is sub welcome and i am first first interview question for you all is that when i am writing function inside a function can we use this message variable over here and this is all about closures guys whenever we write closure i'll give you the answer yes we can definitely access it how now just see this okay this is very important i have defined a main function and then i have a sub function remember you also have to return what is actually returning from the main function itself so i'm just going to return my sub welcome class okay so that is my sub function that is present inside this main function so once i execute it now you see this when i write main welcome and if i execute it i am probably getting this but it's okay let me uh quickly see what i have done mean welcome well welcome um okay sorry i i missed one thing guys that is writing this particular function over there now if i write main welcome so i'm going to return now the sub welcome class and then i'll just execute it now let me call the main welcome class so my main welcome class is over here if i execute it here you'll be able to see that whatever message i have passed over here this is being printed inside the subclass so that basically means inside a closure when you are writing the inner function the inner function will be able to access all the variables that is defined in this particular function even though if this is passed as a parameter let me remove this and let me just consider that this is passed as a parameter and if i write over here as krish right so here you'll be able to see that uh it is basically giving me an error saying that main welcome takes zero position but one was given okay let me just execute it once again sorry i missed executing it okay so here you'll be able to see that welcome to krishnak's youtube channel krish please subscribe krishna so this is an example of closures in this closures what you do is that you will be able to provide whatever as your input on the main chat main function and suppose if you are defining your inner function inside that specific function you can also use that particular variable that is available in the main function and i'm not saying that main function python main function i'm just saying that parent function that is present over here so remember in closures you write a parent function and then inside that you can also write a sub function okay so the parent function and the sub function and whatever variables you can define over here that all will be accessible inside the sub function this is the most important thing that you need to understand about closures now let me just make some more changes okay instead of just calling like this instead of just calling like this let me just write something like this okay so suppose i can i i really want i'll just copy this entire thing okay i'll go down okay and let me make it as a code and over here i'm just going to paste it over here now instead of passing message what i'll do is that i'll just pass function okay a specific function and this function i'll just try to call it now see this okay i'll just try to call it over here now let me just execute it now let me call this main welcome and here i'm just going to call the print function now here you will be able to see that welcome to krishna youtube channel please subscribe krishna but this has still not been able to we are not able to call it but one thing that i missed was my parenthesis now if i try to execute over here so nothing is getting printed now let me just write some parameter so i'll say krish uh this youtube channel deals with programming suppose i'm just writing this particular message and let me just execute this and let me just execute it now you can see that whatever inbuilt function that i am passing whatever inbuilt function that i am passing over here based on that this particular function can also be called inside the sub function okay so here you will be able to see that i am passing func func is nothing but your print statement print is an inbuilt function and then here inside funk you are basically giving your parameter which is basically that the youtube channel deals with programming okay now we will be slowly going into decorators just don't worry because now this is not just closures this is the starter of decorators we'll understand guys in decorators we basically say that inside a function we can also give a parameter as function instead of a specific variable or a message kind of stuff over here this is a perfect example because here i'm actually giving you a function okay now now see this now listen to this very important thing that i really want to call let me let me i'll still not say decorators see i'll just say this as a closures because decorators have some basic changes um i'll say initial declaration suppose initial decorators because we have still not come into decorators now instead of writing in built functions suppose if i write len lenin is also inbuilt function suppose i i say that instead of this print func of funk off and here i'm just going to give you the list of elements suppose 1 comma 2 comma 3 comma 4 comma 5 comma 6 obviously i know that when i'm passing this function if this function is nothing but length length of this particular will basically give me the length of this so here you can see that okay it's not working let me see why it is not working because if i write length of probably a list one two three four five six seven then i usually get seven um let me just see okay [Music] one more bracket okay now this was the thing that i did guys i did not put one more bracket so i was not getting that output but here now it is getting executed now this is with respect to an inbuilt function okay now it is just with respect to an inbuilt function what if can i create my own function and give it as a parameter over there so that also will try to have a look so i'll i'll create a function saying as okay definition of channel name suppose this is my channel name and inside this channel name okay inside this channel name i'll say that print okay i'll say this is krish channel this is chris youtube channel see i'm just making a you know function within a function just see this it'll be pretty much amazing i'm again going to copy this and now i'm just going to name this as decorator see this i'll just remove this and i'm going to just make it as decorators now this channel name i'm just saying that this is chris youtube channel okay now see very very nicely you see this because how to call this channel name inside this and how do you basically display this information also that also will try to check it out so guys after i have created this channel name so here i've clearly written that this is krishnak youtube channel and let me make this change also guys because we are not calling any inbuilt function so i'm just going to call this function over here like this okay i'll just execute this i'll just execute this now see this how do i call this with the help of my how do i call this particular function inside this so here i'm just going to say main welcome main welcome and here i'm just going to give my channel name and once i give my channel name now just see what will happen once i execute it here you'll be able to see that this particular parameter this particular channel name is passed over here after this after this you will be able to see that this is basically calling sub welcome class here it is saying welcome to krishna youtube channel this is krishna kris youtube channel and then please subscribe krish channel so this function is basically defining whatever function we have defined in our custom basis right so decorators understand this this is called as decorators where you're calling function within the function and then you are also passing function as a parameter this is one way of calling guys suppose if i make this variable as a and if i try to execute what is a here you can see that it is not returning anything but if i just go and see a it will give me an error saying that non-type but just understand that we have called something if you are returning something some variable will get stored here this is just like a decorator now this is one way of calling this particular function the other way is basically to define a decorator see we can write at the rate and we can just write main underscore welcome that's it when i write like this this way the initialization will happen this way we will be calling the main underscore welcome function with your channel name as your parameter so once i execute this here you will be able to see that welcome to krishna youtube channel this is chris youtube channel please subscribe channel now the main importance of the decorator over here is that your obviously closures are there right function within a function but apart from that you are also able to pass the parameter as a function and apart from that also if you have a lot of functionalities like this you can call this as your decorator on top of your function suppose if i write channel underscore name over here which is my predefined function i am basically giving a decorator which is basically calling the decorator function and this decorator function whatever function name we have given over here this takes as a parameter and whatever functionality are there we will also be able to use this function directly over there you know with all the information so this if you have some basic and what is the real importance of this just understand it suppose you have this particular function and this function needs to be called with uh as a common functionality in most of the functions so suppose this function is just like a database connection that is basically happening okay and you are basically providing all the information by just using the decorator whatever the basic information are there and this function is going to get reused everywhere you know so at that time you can just call this function like this and you can use it over there you know this is the most important thing now why i'm saying in flask and xango probably will have this because you there you have at the rate app.route that is also kind of decorator and this is how you manually create a decorator so let's revise this guys what what all we think what all things we learned first is about function copy suppose if i am defining a function if i am assigning it to some other variable and if i delete the function then also we'll be able to get the copy of that particular function so here you can see apart from that we saw closures functions in within a function over here i defined a main welcome function then inside i defined a subwelcome function and i had returned the sub welcome function from this particular main welcome function and in by default this will be printing all this particular statement we saw that then closures and initial decorators what we did is that instead of just calling function instead of calling just variables like this uh what we are doing is that we are also passing functions as a parameter and we were using that function for some other purpose initially we started with some built-in functions like print l length and all so we saw that it was also getting executed the next step was towards advanced decorators wherein we are passing this particular information and we are using that particular function but the way that we call this particular function and provide as a parameter was something like this main underscore welcome and channel name this is exactly decorators because this is your decorator class this is the decorator class which is using this particular channel underscore name as your parameter inside that specific function and if you want to call it in a shortcut way you just have to you add the rate and with the decorator name on the top of the function which is getting called okay if channel name is getting called everywhere so you can basically use this particular decorator which is taking as a function as a parameter you can assign it over here so i hope you like this particular video please do subscribe the channel if you're not already subscribe i'll see you in the next video have a great day ahead and please make sure that guys you subscribe the channel bye bye have a great day
Info
Channel: Krish Naik
Views: 9,414
Rating: 4.8666668 out of 5
Keywords:
Id: zA53Lf-fqcQ
Channel Id: undefined
Length: 17min 1sec (1021 seconds)
Published: Sat Jan 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.