Check out a free preview of the full Intermediate Python course

The "Classes" Lesson is part of the full, Intermediate Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina introduce classes, and how the self keyword is used inside classes to refer to a bound instance variable or object.


Transcript from the "Classes" Lesson

>> Nina: To talk about objects, let's talk about classes.
>> Nina: So,
>> Nina: Everything in Python is an instance, all the variables that we've been working with have been instances of a class. So,
>> Nina: For integers, to make a new one I could just pass in a number to the int constructor.

And if I check the type of that we'll see that it's a class int.
>> Nina: Something we haven't done is check the type of int itself. It's a little bit different, the class is a type.
>> Nina: Classes versus instances, this OOP programming paradigms structured around them. You can think of a class as a type of something, here the type is an int, or you can think about specific incidents of something like a car.

Or I'm sorry, you can think of a specific class, or category of something, lIke a car. And then the instance is the specific instance of something. So here the int is the type, it's a class. Whereas the int(5), this is now an instance, this is the number 5.

So if my class was a car you might think of it being a specific thing like my Subaru.
>> Nina: All right, a class is kind of like a template or a recipe and then you can use that to make new active instances that have specific values in them.

When you change the class variable that's only gonna change what's returned from that variable for everything. But if you change the value of something in an instance, you're not changing the type. You're not changing the recipe. You're only changing that particular instance that you have. So I'm gonna come back to this later.

It's a little bit confusing but those of you who've been doing Python for a while, you might have seen this soft key word, peppered throughout code. So let's talk a little bit about what that does. If I try to just put code in a new file, no syntax highlighting.

So I have to save it.
>> Nina: I'm gonna save it as a
>> Nina: Okay, so my class Car, I have a class method her runs = True. And start method here takes the name or it takes the self.
>> Nina: And I also wanted to take a name for my car.

>> Nina: This time I wanna show you that, so self is associated with the instance. If I wanted to save the name that I pass into this method, I would do that by saying = name.
>> Nina: And now I can say if self.runs: print("Car is started"), and I'm gonna put my f in here this time and the name of the car.

>> Nina: Because name is passed into here, we know that we'll have it. But if we wanted access to it in a method that didn't necessarily take this as a parameter, we can use That'll will give other methods access to it. And then I can say, otherwise print the f-string, put the f in there now,, car is broken, no.

>> Nina: We'll talk about imports a little bit more later. I might have to restart my REPL, in order to pick up this new file. Now, I should be able to do from cars import Car. And if you all put those cars file in your top-level directory you should be able to do that as well or so,

>> Nina: Okay, now, I can say I can check the type of car, it's of class type. I can look at car and see the value of runs because that's a class variable. But if I try to check, there's no attribute name that's a instance attribute. So I'm gonna make a new instance and say my_subaru is a new car.

The type of my_subaru,
>> Nina: It's a instance of class Now, I can say in my_subaru.runs and passing the string subaru. What is going on? Pull object is not callable.
>> Speaker 2: You wanna call start.
>> Nina: Thank you so much. Such a total brain fart. Okay, my_subaru.start("Subaru"). There we go, ta-da.

So my Subaru car is started. I know that when I tried to check the type, so, I got an error. What if I ask for, what will I get? The Subaru, right? So that variable is associated with the instance, not with the class type.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now