Class Variables And Class Methods In Python


Hello all my name is krishna and welcome to my youtube channel so guys today in this particular video we are going to discuss about class methods and class variables now we are deep diving into classes things guys and we are we have learnt a lot of things i've also started oops concepts in my python playlist itself now after this will be going towards more advanced things so please make sure that you understand what is the basic difference between class methods and class variables and what are things we'll try to see over here right it'll be pretty much amazing make sure that you watch this video till the end you'll definitely learn a lot of things now to begin with what i have done is that guys i've just created a class car i've created a variable remember this is just like a variable okay if you want to create a variable you just use a variable name you assign some values i'll tell you what is the importance of this particular variable over here i have taken the base price of this particular car or whatever object of this car will be created the basic base price will be this much okay now after this what i'm going to do is that i'm going to create an initialization constructor okay after this the initialization constructor will be my windows doors power okay how many windows are there how many doors are there what is the power of this particular car after this i'm writing self dot windows is equal to windows self dot doors is equal to door self dot power is equal to power remember these are my instance variables okay that is getting created now after this i have also created a cl a function which is present inside this particular class which is called as what's the base price and i've just printed that the base price is this particular value that is self dot base price remember in order to access this class variable there are two ways one is by creating one instance suppose if i write car car 1 is my instance and suppose if i write like i have 4 comma 5 comma 2000 okay this is the variable that i am initializing and if i write car 1 dot base price if i execute like this then you will be able to see that the base price will be able to get it this is one way the other way is basically to use the car clash directly and then directly use the base price if i also use like this i will be also able to access this particular class variable but usually what happens is that what is the best way what we should do and all i'll tell you in just a while but just understand if you really want to access this variable you have two ways either by creating an instance or directly calling the car class itself now remember let me just consider one scenario okay i'm just going to delete this because uh i i just want to tell you one scenario suppose right now in 2021 the base price is this much now probably in the next year when 2022 will come what i want to do is that we need to add an inflation of the base price by 10 okay i'll just give you this particular scenario okay and remember in 2022 whenever we try to create an instance of this by default the base price should be the recent price once we update it you know uh by 10 percent itself which is basically the inflation rate i'm just taking this as an example so to do that we introduce something called as class methods okay and in order to use or create a class methods we basically use something called as uh uh decorator which is called as add the rate class method understand the scenario right now guys what i want to do is that every year i want to add 10 percent of inflation rate this is my condition over here and in order to add it on the class variables we will just not assign it somewhere we will just not change the values inside a function but instead we'll try to use the class method itself over here now here i'm going to define the class matter and i'm going to say definition revise base price suppose this is my function that i'm creating which is again a class method itself and here i'm going to give two parameters one instead of giving self parameter now i'll give the class parameter this class whenever we are defining the class method guys we have to provide this cls parameter whenever we supply self that basically says that it is with respect to the instance of this class whenever i provide cls this is basically with respect to the class type okay this is basically this is directly referring to this particular class like how we access car dot base price right this cls will be referring to this particular class now apart from that i will also tell my inflation rate so i'll say my inflation rate what is my inflation and here now what i am saying that as every elect right now it is 20 20.

Suppose we go to 2021 i need to change my base price probably i'll be changing my base price with the help of class method and this will be very very handy when i'll show you the example of initializing things just give me a second with for that now suppose i want to update this particular base price so i'll write c l s dot base price is equal to please listen to it it is very very important so i'll write cls.base price plus inflation okay plus inflation i'll not just write plus inflation see i'll say that cls dot base price multiplied by inflation okay suppose my inflation is 10 percent i need to find out what is the 10 of this particular value and that value i'll be getting it over here and then i'll be adding that to my base price so this is what my class method is doing okay if i want to change this value such that in 2022 if whenever i make an instance of this particular car class the base price should be the updated price or the revised price and for that i'm using this class method see this guys now let me just define car one is equal to okay car one is equal to some uh values over here like 4 comma 5 comma 2000 suppose 2000 is my power now if i execute this over here suppose my car 1 dot base price is something like 1 lakh inr okay now remember suddenly now we went into 2022 obviously uh 20 in 2022 i told you that because of the inflation we definitely need to increase the base price so what i'll do is that instead of just creating this object i'm just going to delete it instead of doing this you know what i will do is that i will say car dot revise base price and here i'm just going to say that this is my inflation rate 0.

10 now once i execute this so here i'm probably getting an error saying that name inflation is not defined why uh okay so this will be my cls uh inflation inflation oh sorry the parameter is a little bit wrong over here so i'll just make it as inflation let me execute this once again and here now let me just execute this there was a small smelling mistake so here you can now see if i write card dot base price it is showing one one lakh ten thousand see there is an increase in point one zero now whenever i create an instance saying as car one is equal to car with uh four 5 comma 2000 and if i try to execute it now if i try to see car 1 dot the base price here you will be able to see that it has got updated it has got updated see i can also call this particular method by using my uh my my instance like car one i can definitely call it if you want to see this particular example see this i'll say that okay car one dot revise base price again i want to increase it to ten percent okay now see this this is also a way to call it but in the real world practice this is not used guys whenever we want to do an updation with respect to the class variables we may use class method and always it is done by using this particular technique where we are using the class name dot that particular function name yes we can do this this also see in if i now go and see my car one dot uh what is my base price here you can see that it is got still updated and whenever i create my next instances also suppose if i create my next instance like class three suppose i have three comma four comma two thousand.

Right even though i create over here and if i try to see the base price the base price will also get updated it is also possible by the instance but usually in the real world scenario by considering the good practice it is not done in that particular way but i hope you have understood why do we use this particular class method and how it is basically used why we have used cls over here specifically when we are using this cls and when we are updating this particular values once it is updated then anytime i create any new instance that new value of that particular base price will be updated now here in this particular case even though i use this instance where i change my base price by again 10 percent my base price was this much then again i created a r3 instance and then that time you could see that my new base price was something like this right this you can also call it like this or you can also call it like this but in the good practice where i've seen many good developers that i've seen projects they basically use this particular technique right so i hope you have got an idea of class method what is this this is basically a decorator try to see some of my examples in decorator which i've explained in my previous session my complete python playlist but now i hope you have got an idea what is class variables and what is class methods class methods definitely can be used to update the class variable only once so that whenever you're creating the newer instances automatically that value will get updated now i don't have to do anything you know as the year changes i'll just go and execute this revised base price again and again that whole base price value will get changed and then after this i have any kind of method i can do whatever i want okay so i hope you like this particular video please do subscribe the channel if you're not already subscribed i'll see you in the next video have a great day thank you bye.