Advance Python Series-Magic Methods In Classes
Hello all my name is krishnak and welcome to my youtube channel so guys we will be continuing the advanced python series and in this particular video we are going to discuss about magic methods and classes now why it is called magic because as you know there is no such thing as magic right something some trick is actually there in the back end but still we say yes you can do some kind of magics i think you have seen so many magicians who will be taking out vision will be taking out some or the other thing from their hands right so similarly over here why we call this as a magic methods because something is happening in a tricky way okay so let me just show you if you're new to this video please do subscribe the channel press the bell notification icon and let's begin this particular session to begin with guys first of all what we are going to do is that i have created a car class and in this car class we obviously have a initialization constructor and then we have created four variables that this particular car object may be taking so windows doors and engine type and then i'm initializing all these particular variables and then after that i also have a class method which is a public method also and this all variables are also public with respect to access modifiers and this function is basically saying that the person drives the car so drive is a functionality of a car and this is basically a car blueprint now what i can do is that if i write and this is i think you've seen this particular code a lot okay suppose if i write c is equal to car so i'm just initializing the object and this i'm just going to give the values like 4 comma 5 comma engine type that i'm going to give is diesel okay so once i execute this here you will be able to see that first of all let me just execute this once again okay and then if i create this particular object i can see this object see that has been created initially you are getting that specific message i'll tell you why we had got that specific message but just before recording this video and made it some changes over here now here you can see that it basically specifies that this is an object of this particular car at this specific memory location right remember one thing guys this is your initialization constructor this is your method that is present within the class now if i write dir of c right so here you can see all these attributes that are present this all that you can see underscore underscore class underscore underscore underscore underscore dick underscore underscore so all these attributes all these methods that you actually see are actually called as magic method in class why because just understand we are trying to create a object here right we are trying to create an object here and how it is actually happening internally internally through some magic right this underscore underscore init is basically getting called okay again i'm telling you guys in if you really want to create any objects you basically create an object of a class by using that particular class and providing these particular values that are present in it right and this underscore underscore and it underscore underscore is called from the back end right as a times of magic and then you are actually creating an object so underscore underscore init is basically present over here and this is called as the magic method now let me just say show you some more examples suppose i have this particular method that is underscore underscore str underscore underscore and remember guys we can override this magic methods too we can override it so let us take one example where i'm using underscore underscore str underscore underscore now see guys.
If i just print c by default i am getting the specific message let me just write print c okay if i am printing see it is saying that okay it is a car object at this specific memory location the reason it is being able to print this message because by default by default there is a magic method whenever we write print a class object and if i go and see direct of c it is in short calling this underscore underscore str underscore underscore method this is the magic method being called and by default this function or this method displays a message saying that this is an object with respect to some memory location now if i really want to override it what do i do i will just write something like this see definition underscore underscore str underscore underscore and here inside that i'm will be taking a parameter self and here now i can override my functionality i can say that okay this in just a second so i will say print the object has been initialized something like this i'm just printing some generic message let me just write this in quotes okay just to show you that yes we can overwrite this magic method we can make it display something else now once i execute this now this in short says that you underscore underscore str underscore underscore is being overridden and what is the functionality of this whenever you are trying to print a class object by default whatever is present over here that is going to get printed if you have over read it by default it shows you this car object now see this again i'll try to execute this and again i'll try to say print c okay now it is giving me an error saying that underscore underscore str return non-string now see guys this string is getting printed but we cannot print it inside it so we have to return it because this method this magic method returns something okay it may be a string it may be something right so i'll just execute it once again and just change from print to return now let me just print this particular c now when i print the c the previous functionality will be overridden by this and remember guys you will be only able to see when you are using this print functionality see this once i execute it here you will be able to see this one if i try to just print or if i just try to display c it will tell you that okay it is a object of a car remember the print statement is getting overridden because we have overridden this particular method which is this magic method so here if i go and write print see so here you will be able to see that object has been initialized so this is the message that is getting displayed now similarly i may have many methods let's take one more magic method okay so magic method i can take something like this let's see so there is something called a size off okay now if i really want to see what is the size of this particular object i may use something like this c dot underscore underscore size off and if i just try to print this method you will be able to see that i'll be getting 32 this basically says that this entire object is consisting of 32 bytes now if i want to overwrite this all i have to do is that i have to basically override this functionality saying there's diff underscore underscore size of underscore underscore and here i'm going to use sorry i am going to use self okay self and here i'm just going to say return the overall size is the overall size is i'll just use a placeholder and use some advanced formatting saying that dot format and here i can say that whatever the size is right the size is probably uh this this size i can actually print it somewhere or just let me just write as generic message this displays the size size of the object something like this i'm just displaying some generic message guys so that you don't have confusion in understanding it so once i execute it i execute this print c will obviously tell me the object has been initialized now if i try to execute this here you'll be able to see that this displays the size of the object this is how we override now why it is called as a magic method because we are just calling this right we are just calling this functionality internally something is happening like suppose if in this particular case i'm creating an object internally this init method is called if i am printing this internally this is basically getting called like that i can have a lot of functionalities let me write one more functionality guys there is something called as underscore underscore new let's see where it is or this method now this underscore underscore new is basically called before init if you don't believe me just see over here i'll just write definition of underscore underscore new underscore underscore and here i'm just going to use all these four parameters whichever i'm actually calling and i'll say that print the object is getting initialized something like this the object has started getting initialized something like this guys now whenever we create an object first this method will be called and then init method will be called if you don't trust me see over here guys i'll execute it and here you can see that the object has started getting initialized so this has got executed then the initialization constructor gets executed now i hope you are understanding why we call it as a magic method because some magic happens in the back end that is nothing but some trick where it is helping you to create an object in an efficient way so i hope you like this particular video please do subscribe the channel if you haven't already subscribe i'll see you on the next video have a great day thank you and all and yes guys i've started this python playlist the link will be given in the description of this particular video till then have a great day bye bye.
If i just print c by default i am getting the specific message let me just write print c okay if i am printing see it is saying that okay it is a car object at this specific memory location the reason it is being able to print this message because by default by default there is a magic method whenever we write print a class object and if i go and see direct of c it is in short calling this underscore underscore str underscore underscore method this is the magic method being called and by default this function or this method displays a message saying that this is an object with respect to some memory location now if i really want to override it what do i do i will just write something like this see definition underscore underscore str underscore underscore and here inside that i'm will be taking a parameter self and here now i can override my functionality i can say that okay this in just a second so i will say print the object has been initialized something like this i'm just printing some generic message let me just write this in quotes okay just to show you that yes we can overwrite this magic method we can make it display something else now once i execute this now this in short says that you underscore underscore str underscore underscore is being overridden and what is the functionality of this whenever you are trying to print a class object by default whatever is present over here that is going to get printed if you have over read it by default it shows you this car object now see this again i'll try to execute this and again i'll try to say print c okay now it is giving me an error saying that underscore underscore str return non-string now see guys this string is getting printed but we cannot print it inside it so we have to return it because this method this magic method returns something okay it may be a string it may be something right so i'll just execute it once again and just change from print to return now let me just print this particular c now when i print the c the previous functionality will be overridden by this and remember guys you will be only able to see when you are using this print functionality see this once i execute it here you will be able to see this one if i try to just print or if i just try to display c it will tell you that okay it is a object of a car remember the print statement is getting overridden because we have overridden this particular method which is this magic method so here if i go and write print see so here you will be able to see that object has been initialized so this is the message that is getting displayed now similarly i may have many methods let's take one more magic method okay so magic method i can take something like this let's see so there is something called a size off okay now if i really want to see what is the size of this particular object i may use something like this c dot underscore underscore size off and if i just try to print this method you will be able to see that i'll be getting 32 this basically says that this entire object is consisting of 32 bytes now if i want to overwrite this all i have to do is that i have to basically override this functionality saying there's diff underscore underscore size of underscore underscore and here i'm going to use sorry i am going to use self okay self and here i'm just going to say return the overall size is the overall size is i'll just use a placeholder and use some advanced formatting saying that dot format and here i can say that whatever the size is right the size is probably uh this this size i can actually print it somewhere or just let me just write as generic message this displays the size size of the object something like this i'm just displaying some generic message guys so that you don't have confusion in understanding it so once i execute it i execute this print c will obviously tell me the object has been initialized now if i try to execute this here you'll be able to see that this displays the size of the object this is how we override now why it is called as a magic method because we are just calling this right we are just calling this functionality internally something is happening like suppose if in this particular case i'm creating an object internally this init method is called if i am printing this internally this is basically getting called like that i can have a lot of functionalities let me write one more functionality guys there is something called as underscore underscore new let's see where it is or this method now this underscore underscore new is basically called before init if you don't believe me just see over here i'll just write definition of underscore underscore new underscore underscore and here i'm just going to use all these four parameters whichever i'm actually calling and i'll say that print the object is getting initialized something like this the object has started getting initialized something like this guys now whenever we create an object first this method will be called and then init method will be called if you don't trust me see over here guys i'll execute it and here you can see that the object has started getting initialized so this has got executed then the initialization constructor gets executed now i hope you are understanding why we call it as a magic method because some magic happens in the back end that is nothing but some trick where it is helping you to create an object in an efficient way so i hope you like this particular video please do subscribe the channel if you haven't already subscribe i'll see you on the next video have a great day thank you and all and yes guys i've started this python playlist the link will be given in the description of this particular video till then have a great day bye bye.