Intermediate Python

Practice: Classes

Intermediate Python

Check out a free preview of the full Intermediate Python course

The "Practice: 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 live-codes the practice where classes and class instances are reviewed.


Transcript from the "Practice: Classes" Lesson

>> Nina Zakharenko: For this third exercise on object oriented programming, we're gonna learn about classes and self instance variables, inheritance, and also using the is subclass and is instance methods, to learn a little bit more about our class structure. This time around we're going to be making files. So I'm going to make a new file and I'm gonna save it as, just in the root here.

And we're gonna practice making a simple class. We're gonna pass in several variables and then we're going to save them to the instance by using self. So we'll type class Vehicle, define that __init__ class, and what do I have to pass to __init__?
>> Speaker 2: Self.
>> Nina Zakharenko: Self, even if I accept no arguments self always has to be there.

But we will take a few arguments, we're gonna take a make, a model, and a fuel is going to be gas.
>> Nina Zakharenko: And to save those values to our instance we're gonna have to do a self.make is equal to make, self.model is equal to model, self.fuel is equal to fuel, okay?

Yesterday we talked about main method and when we would wanna put our code in the main method. But just a reminder it's not required, so for this simple exercises am just going to put the code where I used the class I defined just right underneath here in the same file.

Just remember indentation is important. So this indentation level belongs to this method. This indentation level where def is belongs to the class. So if we're gonna want to use this class, we're gonna have to break out of that class's indentation level, and I'm gonna say that my daily car is a vehicle and it's a Subaru Crosstrek

>> Nina Zakharenko: And let's take a look at what this prints out too. So remember the fastest way to run this in Python in VS Code is just to say Python:RunPythonFile in terminal. And we see yuck, prints out that is vehicle object and you see this dunder main here? We talked about why that is in the introduction class, can anyone remind me?

That gets set when we run this script directly, so that's the same dunder main that you check for if you wanna have a main method. And this is a memory address, so nothing useful at all. But we do have access to the variables, and so I'm going to comment this out, and instead I'll print out an f string and I'll say, I drive,

>> Nina Zakharenko: Daily.make,
>> Nina Zakharenko: Daily.model,
>> Nina Zakharenko: It runs on,
>> Nina Zakharenko: Daily.fuel.
>> Nina Zakharenko: Okay, if I run this.
>> Nina Zakharenko: Okay, a little bit of an improvement, right? It says I drive a Subaru Crosstrek and it runs on gas. And I'll just put a period there to finish our sentence. Remember that the better way of doing this is to create a wrapper.

>> Nina Zakharenko: Or a str.
>> Nina Zakharenko: So, let's make a quick str method here, and then I'll take a self. Why don't we need to pass in any of the other arguments?
>> Nina Zakharenko: Make, model, fuel.
>> Speaker 2: It's already implied within that self attached to that.
>> Nina Zakharenko: Right, it's attached to the self, it's attached to the instance.

So now I'm just going to cut the string here and paste right here. But this isn't going to work, why? There's two problems.
>> Speaker 2: Got to return it.
>> Nina Zakharenko: I need to return it, so str needs to return a string. What's the other problem here?
>> Speaker 2: Daily is out of scope, you want self instead of-

>> Nina Zakharenko: Right, daily is out of scope, we want self. So self.make, self.model, self.fuel.
>> Nina Zakharenko: Now I'm gonna uncomment my printdaily,
>> Nina Zakharenko: And should see the same result. So instead of having to conjure up this long string every time I want to represent my vehicle, I can just use __str__ instead and then it's in one place.

Okay, so we talked about class variables as well. Variables that exist for all instances of that class whether or not, it exists on the type as well as all instances of that class. So you don't even have to have an instance to use it and see it. So we're gonna make,

>> Nina Zakharenko: A number_of_wheels class variable here and I'm gonna say 4.
>> Nina Zakharenko: And now I can print out my daily.number_of _wheels and I’ll just say this is the instance,
>> Nina Zakharenko: For instance. I can print out the like class or type variable as well.
>> Nina Zakharenko: How do I access that class, not an instance, just the class.

>> Speaker 2: Vehicle, capital V.
>> Nina Zakharenko: That's right, capital V vehicle.
>> Nina Zakharenko: Okay, I'm gonna run this. See for the class it's 4, for the instance it's 4.

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