Check out a free preview of the full Intermediate Python course

The "Practice: Inheritance" 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 subclasses and the super keyword are reviewed.

Preview
Close

Transcript from the "Practice: Inheritance" Lesson

[00:00:04]
>> Nina Zakharenko: What if I say daily.number_of_wheels=3? And I copy and paste these two print statements
>> Nina Zakharenko: All right, for the class, it's still 4. But now for this specific instance, and I should say for this instance daily, now the number of wheels is three, right? Okay,
>> Nina Zakharenko: So inheritance.

[00:00:41]
>> Nina Zakharenko: Let me close this REPL window. I have my class vehicle. Let's make a new class. And we don't necessarily want the vehicle class to tell us how many numbers a wheel has. That's more of an implementation detail. So I'm gonna take that away for now. Instead, I'm gonna make two new classes.

[00:01:02]
I'm gonna make a class Car(Vehicle) and inherit from vehicle. And it's gonna have a number_of_wheels = 4.
>> Nina Zakharenko: Notice that last time when I showed you inheritance I showed making a new init method for that child class. But I don't need one here, if I don't provide an init method in the subclass, which initializer is it going to use?

[00:01:41]
>> Nina Zakharenko: If there is no init method here, it'll just call the init method for vehicle.
>> Nina Zakharenko: In the same way that we saw if the vehicle has a variable, it's available to all of its subclasses. Same thing for methods. Python will try to instantiate the car, if it doesn't see it, it will look for the vehicle, and it'll just be that very same one.

[00:02:10]
>> Nina Zakharenko: And let's say we have a class Truck.
>> Nina Zakharenko: It's also a vehicle.
>> Nina Zakharenko: And the truck has six wheels
>> Nina Zakharenko: And it's also, these trucks run on diesel, so this time let's make a custom constructor. I'll say def_ini_, pass in self. I'll take a make, model, and the fuel will be diesel.

[00:02:46]
>> Nina Zakharenko: Okay, this time, I wanna call the init on super, why is that? Looking at the fuel here and the fuel here, why is that?
>> Speaker 2: Cuz we need to override that value?
>> Nina Zakharenko: Yeah, I'm overriding this default value
>> Nina Zakharenko: And so super.init, we're gonna pass in the make and the model, and the fuel.

[00:03:17]
>> Nina Zakharenko: Okay, now let's take a look at this. And I wanna show you something that's not in the class website. But as a pretty cool feature of Python. I can open up my Python tab here and VS code. And if we press the up key, we'll see that this is the command that VS code automatically filled in to run my script.

[00:03:49]
So it's got the full path to my python3 virtual environment and then it's got the full path to my file. I don't necessarily need all of that. I can just say Python in my virtual environment and pass in the path like that.
>> Nina Zakharenko: Oops, sorry and only to comment out my old code here because the vehicle no longer has the number_of_wheels.

[00:04:26]
>> Nina Zakharenko: Handy shortcut to coming out a bunch of lines in VS code, at least for OSX, you can just select them and press Cmd+/. And if you select commented out code and press it again, it'll uncomment it. Okay, let's try that again.
>> Nina Zakharenko: Okay, this works. A really cool thing that Python can do is called run in interactive mode, this is great for debugging and it's not in the course website so if you wanna know this, write it down.

[00:05:01]
All you need to do is pass the -i flag after python before your script. What this is gonna do is run everything in this python file and then it'll drop you into REPL. So you can take a look at the state of everything. I see here that I made the daily variable.

[00:05:22]
Now, I can look at that right in my REPL. So really nifty thing to know when you're just debugging.
>> Nina Zakharenko: Now, I'm gonna show you some of our new instances. So I'm gonna say my daily driver is a car and it's a Subaru, the make is a Crosstrek.

[00:05:51]
>> Nina Zakharenko: And now I can print out an up string, "I drive (daily.make},
>> Nina Zakharenko: (daily.model).
>> Nina Zakharenko: It uses {daily.fuel)
>> Nina Zakharenko: And drives on daily.num_wheels, okay?
>> Nina Zakharenko: What did I do here, daily.make.
>> Speaker 2: Number_of_wheels.
>> Nina Zakharenko: Yes, number_of_wheels. Thank you. Okay, there we go. Drive Subaru Crosstrek uses gas and has four wheels.

[00:06:44]
I can do the same thing for truck. I can say my truck is a Ford F350, so awesome cool truck. I can check the truck.make, truck.model, what's the truck.fuel going to be?
>> Speaker 2: Gas.
>> Nina Zakharenko: It's not gas. So if I look at where I-
>> Speaker 2: Diesel, I forgot what the default was.

[00:07:09]
[LAUGH]
>> Nina Zakharenko: That's It's okay, so my truck fuel is gonna be diesel. All my trucks are gonna be diesel, pretty useful, right?
>> Nina Zakharenko: Using type as instance and this subclass, so the type of my truck is a truck. The type of my daily driver, [SOUND] I really can't type today, there is no truck method here.

[00:07:38]
It is type of daily. Okay, so it's car. I can ask, isinstance(daily, Car). What will I get? Yeah, true, right? Isinstance(truck, Truck), yeah. And just a side note here. I know that this truck is my lowercase variable, my instance. And I know that this capital Truck is my class kind of just by looking at it from naming convention, right?

[00:08:14]
It's one of the reasons that convention is in Python or so powerful. Awesome, now, I wanna ask issubclass(Truck, Vehicle)?
>> Speaker 2: Yes.
>> Nina Zakharenko: Yep, and issubclass(Car, Vehicle)? Yeah, what about issubclass(Vehicle, Car)?
>> Speaker 2: Nope.
>> Nina Zakharenko: Nope.

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