Class Methods, Static Methods, & Instance Methods EXPLAINED in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
there are some things in Python that I learned extremely late and one of them was the difference between a static method an instance method and a class method I just didn't run into them that often to be able to understand the difference and one of the most common places you'll find for example a static method is when you're typing some random method inside your class and it gives you this squiggly line that you want to go away and that's going to tell you that method add numbers may be static and when you click on that button it ruins your code and takes that method outside of your class and you don't even know why it does that but anyway this video is going to teach you the difference between an instance method a class method and a static method so that you can use those comfortably in Python when the time comes now we're going to do this all from scratch so you'll get a maximum understanding of this concept and we're going to use two modules for this that aren't related to actually creating a class or an instance but we do need them to make my code feel more at home so here we'll type in from typing import self and this is new in Python 3.11 you don't need it but I'm going to use it and from date time we're going to import date time or just date actually now first let's talk about instance methods and static methods and to do that we will create this calculator so calculator and we'll create an initializer and this initializer is going to take a version number so version and all we're going to do is initialize the instance variable with that version number then we will create an instance method so here we'll type in Def description and all this is going to do is print a description of the current calculator and its number so we will print the following text which I will copy and paste because today is a lazy day so we'll paste that inside there that we are currently running the calculator on version self.version which is the version that we initialize this calculator with and this is considered an instance method because we are referring to self which refers to this instance over here which means if we create a new instance from this calculator we're going to be able to use that version from the instance not from the class and to demonstrate that we can create two calculators we can say calc one is going to equal a calculator with the version of 10 and we'll duplicate that say calculator 2 will have a version of 200. now I will wrap this in if name is equal to Main and we're going to print the description of each one of these so calculator one has its description and calculator 2 also has its description and if we run this now we will get the descriptions back from those instance methods as you can see each instance had its own local instance variable so we were able to retrieve those by using the self keyword and we're all familiar with how instance methods work because we can directly modify this instance thanks to the self keyword so where do static methods slip in and I will show you right now by creating a new function and this function here is going to be called add numbers and what we want to get is an undefined amount of arguments so the user can enter as many numbers as they please of type float and we will return to them the sum of these numbers so it's a very simple function that does what we want it to do but it gives us this crazy squiggly line and it says that method add numbers may be static and as I demonstrated earlier if you click on that quick fix it's just going to do what it wants it's going to put it somewhere that you don't want it to be or maybe you do want it to be there but I doubt it so to explain what a static method is a static method is just a method that can be used anywhere that doesn't rely on the class so ad numbers can effectively be outside of this class and it won't affect anything because we do not use the instance inside this function and we can even remove that and it will be perfectly fine and this will just be part of the class as a static method but usually it's nice to annotate it as static method just to inform others that this is static and that it is a choice you made to keep it inside this class because again this can be placed anywhere inside your program and it won't affect the class and it will still run properly but a general reason you'll keep a static method inside a class is because it pairs nicely with the class that you are working with so maybe we want to add numbers and you can of course put that in any module you want but maybe it's easier if we just use it here we can say calculator dot add numbers and we do 10 20 30 and you'll notice here we did not have to create an instance to use it we were just able to use it as it was and if we print that sum we'll get 60 back so we just added it to that class for organization and really there's no general rule for this it's up to you how you want to organize your code you can put this in a different module if you want you can make a math module for your code or you can combine it with some class that actually uses that functionality quite often so it might pair nicely with that class as an addition just as I did down here with calculator dot add numbers we can use it anywhere that we have the calculator without having to create an instance from that class so I'm afraid I can't give you any more tips regarding where to use a static method it's really a personal preference whether you want this method to be paired with your class or inside its own module that's up to you and that's the tool that python provided to us to make it explicit that we want this to be inside the class so now we have a good understanding on the difference between instance methods and static methods all we have left are class methods so to achieve that example let's delete everything but the Imports and I'm just going to paste in this class and explain it real quick right here we have a person with an initializer that takes a name and an edge and then we initialize these variables inside our instance so that we can use them later in this description for example now we know we can create a method that affects the instance and we know we can create a method that doesn't affect the instance nor the class so we only have one remaining option inside here to edit the class and that is using a class method so here we can type in at class method and we'll type age from year and you'll notice something funky here that we have CLS instead of self and that's because we are now editing the class directly we're not referring to the instance anymore any changes we do to this class will affect the class the actual class and it's also important to mention that self and CLS are just naming conventions this can be anything you want you can say hello if you want and that will refer to the class but as you can see the editor tries to tell us usually first parameter of such methods is named CLS and that's a naming convention I would follow so I would keep it as CLS if you're planning on having other people look at your code at any foreseeable point in the future but here we have CLS and what we're going to do inside here is create a factory or an alternative Constructor for our person class because right now we can pass in a name and an edge but maybe we have someone who wants to pass in a birth year instead of just passing in the age and that can be much easier done if we just pass in the birth year using an alternative Constructor or this Factory so here we need to still create the Constructor name of type string but this time we can tell the user that they can create a person by providing a birth year and that should be of type integer and it's going to return to us self which is the class itself now inside we can type in that the current here of type integer is equal to date dot today dot year and we're going to get the H by typing h of type integer is equal to the current here minus the birth here that we've provided so make sure you provide a valid birth here or this is going to be a bit funky now what we can do is return that class so we can type in return CLS and we're going to pass everything into the Constructor so here we can type in the name and the age which we provided as a birth year now let's see how we can use this by creating our if name is equal to Main Check scrolling down a bit and I'm going to create a person which is myself so I'm a person and we're going to get the age from the year so here we'll type in my name first as Federico and we will pass in my birth year which is 1997. and we're just going to print my description so me dot description and if we run this we'll get that I'm 25 years old and that was thanks to creating this alternative Constructor or this Factory we still created an instance of a class but this time we did it using a class method so to keep it short and simple a class method is going to affect the actual class while an instance method is going to affect the instance meaning that we can refer to self and the variables inside the instance and a static method might or might not be related to the class it can be used outside or inside and it doesn't have anything to rely on from the class itself so that just about covers everything you need to know about instance methods class methods and static methods of course if I forgot something do feel free to leave that in the comment section down below so others can learn a bit more about what I missed but otherwise with that being said as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 49,911
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: PIKiHq1O9HQ
Channel Id: undefined
Length: 10min 3sec (603 seconds)
Published: Thu Dec 22 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.