Practical Guide to Python Methods & Magic Methods
Transcript from the "Methods & Magic Methods" Lesson
>> Cool, so classes can have class methods on them. Those are methods that are shared among all the instances of a certain type. They can be overwritten in a specific instance or subclass. And in order to do that, you have to add what's called an Annotation over that method, called Class Method, to say that this method does not expect an instance on it.
[00:00:31] Instead you get a variable called Class or CLS or whatever you wanna call it, you should not name it CLASS because that is a keyword. So you might see something like this. If you see this, you might also see it called CLAZZ. Just so that it doesn't override this keyword here.
[00:00:54] And this lets you access some of the variables that are defined in the class. So now I can access CLS dot runs and CLS dot number of wheels. So I know that all of my car CLSs are going to have four wheels. And so this method can be shared between all of the instances.
[00:01:18] We can though just override that in our instance itself. If I make a new my car instance, I can set number of wheels to six. That will only affect that one instance. Now, just like we used type on the repr there are two more helper methods that you can give us is instance, and is subclass to tell you.
[00:01:44] What if a particular variable is an instance of a particular class. So for example, let's try these out. I can ask Python, find know the type of 42, and I know the type of string, I can ask Python is instance 42 int and I should get true. Can I also ask is instance the string hi int?
[00:02:23] No, is instance hi, str, this will return true. And I can do the same thing for the objects that I define. So here, Let's exit out of the repr. I can print out, is instance, my other car, car and run this, and we'll see that it is true.
[00:03:03] So if you need to investigate what's going on with your objects or your instances. Is instance and is subclass will help you figure out those answers. One other method I want to talk to you about in classes is this optional magic method called init. We haven't talked about magic methods just yet.
[00:03:25] Magic methods are defined by having two underscores before and after them. That means that there's something special about these functions, these methods. So in this case, Dunder init, Dunder stands for double underscores, is going to get run when you create an instance of that class. And you can do that Dunder init method.
[00:03:55] You can use that method to define any scenarios or setup that you need when an instance is being created, including setting instance variables, so that you can save what is passed into that constructor to use for later. So let's copy this and take a look at it. This is the my car class just like we had but I've added a Dunder init method here.
[00:04:34] Remember, it's axon an instance so I need to pass in self first. I can also pass an in make inner model. These are required arguments. So if I don't pass these in, instantiation will fail. Now, if I want to save those values to use later. I need to save them on the instance just like this.
[00:05:00] Once I save them on the instance, that means that I can use them later on when I call other methods on them. So here when I call my car equals car Ford and Thunderbird and I call start, I will print out that my Ford Thunderbird is started vroom, vroom.
[00:05:25] There are other magic methods that you might come across. One is Dunder str the other is Dunder repr. These are two handy magic methods that are useful for debugging. You can think of and they both return a string. You can think of Dunder str as the representation of your object that should be human readable, something that you can pass back to the end user to let them know how this object is represented.
[00:05:56] Whereas Dunder repr should return the Python code that's maybe necessary to rebuild the object. So Dunder wrapper is used more for under the hood, a representation for other programmers a representation for debugging. And that's kind of the difference. If you take a look at what is returned from datetime dot datetime dot now, which just creates a new object with the current date and time.
[00:06:26] If you take a look at the str representation, you'll see that there's a date and a time stamp, if you take a look at the repr, it will let you know that this is a date, time dot date time. And these are the arguments that you would pass into that method in order to recreate it.
[00:06:45] These are just conventions. You don't have to follow them but you will see these throughout Python code. Generally you do wanna have a str and a repr in each of your classes. Know that you can also instantiate objects in for loops or in list comprehension. So a lot of these concepts are meant mix and match.