Practical Guide to Python Classes vs Instances
Transcript from the "Classes vs Instances" Lesson
>> There are a few concepts here that probably match what you might be familiar with, object oriented programming language and whatever language that you already know. But an object can kind of be anything, it can be variable a property, a class. Everything in Python is an object. And you can think of an object kind of like a generic container.
[00:00:26] So, a list object might contain a sequence of integer objects, along with some function objects. And the list could have lists in it. Everything in Python is an object. So everything that you might be able to do with Python objects, you can do with any of these things that are objects under the hood, which isn't necessarily the case in other languages.
[00:00:54] Python really buys heavily into this object oriented programming model. But that doesn't really mean that you need to use object oriented programming in your own code. Python works perfectly well as a procedural or a scripting language, where one command is just executed one after the other like a recipe.
[00:01:15] But getting familiar with object oriented programming will really help you level up as a programmer. So a little bit about classes. A really important concept for you to be solid on is the difference between a class and an instance of that class. So, for example, the number 42 is an instance of the class int, whereas int itself is the class object.
[00:01:48] Now, you can think of a class as a type of something like a car, and then you can think of an instance like a specific thing, such as my car the Subaru, which is a type of car. Both classes and instances can have variables and methods on them which is a little bit confusing.
[00:02:11] Changing a variable on the class is going to change what you get returned when you call that variable from an instance. However, when you change things on an instance, it does not affect the class itself. And we're gonna come back to that concept in a second. But I want to cover the self keyword, and you might have seen that peppered in to the examples of python code that you've seen so far.
[00:02:41] Self is used inside of classes to refer to abound instance of that variable or object. So if we have a class car, and we made a specific instance of that class called my car. When we call the start method here to see if that car runs, we pass in the self keyword.
[00:03:10] So let's start a new file named chapter_6.py. And you can just go ahead and copy all of this code and paste it in there. Let's go ahead and run this. We should see some output here it says my_car runs is true. And then my_car.start prints. My car is started.
[00:03:41] What happens if we set my_car.runs to false? And run this again. We'll see that my car is broken. It's very sad. If we make a new instance of this and say, my other car as a car, and if I print out my other car, my other car, And around this We'll see that Oops, sorry, did not want to print that out since this already does some printing.
[00:04:44] So let's run that again and we'll see better results. We'll see that this car is broken. But my other car runs just fine. So when you change properties on an instance, it only affects that instance. And in your Python code, self refers to an instance. So, that means that self, when we created that instance variable on my car, when we called start on it, that self keyword told the interpreter to look for an instance variable in my car called runs.
[00:05:31] And when we called my other car.start, it was attached to that other instance. All instance variables in these classes are going to take self as their first argument. If we try to call start on the car class, instead of on an instance. Let's see what happens. I'm going to get an error here.
[00:06:04] And it's gonna say that start is missing one required positional argument called self. As you write Python code, I promise that you will see this error over and over, so might as well get familiar with it. That's because I'm trying to call this method, which is a instance method.
[00:06:25] It's expecting an instance on the class itself. And that does not work. Let's take a look at the opposite scenario. If I have an instance here called my other car, and I call my car.start. This should work just fine. If I remove this self keyword here, and comment this out.
[00:07:01] And I'm going to put a stab here called pass, which makes this valid Python code without anything in it. When I run this, I'm gonna see that start takes 0 positional arguments but 1 was given. So it's also an error that you'll see quite a bit in Python.
[00:07:21] So make sure that you recognize it. This means that you are trying to call a method on an instance. Which passes self to this method, this class method under the hood, but you forgot to include self here. So calling start on this instance is passing in self under the hood, if you forget to provide it in the list of arguments, you're going to see this error.
[00:07:51] So now that I've added self back in, this should run just fine.