Rock Solid Python with Python Typing Transcripts
Chapter: Orthogonal Typing
Lecture: Inheritance Gone Wrong, an Example
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Remember our friend the duck, duck typing? When we started talking about Python and type hints and many of the things covered in this
0:10
course, what we said was, ""Python used to have duck typing and still does, but it also
0:17
has these additional ways to add typing by saying, 'It takes this type of class hierarchy
0:24
or it takes these types of numbers,' where you specify exactly what it is and they have
0:29
to match in a static language style. The runtime is forgiving when they don't match, but from
0:35
the type system, they're supposed to match exactly, right? But maybe there were some
0:41
benefits to the duck and, well, see you later, duck. We're going to miss you. You won't be
0:47
missing him for long because duck is coming back. But let's go through a little thought experience to see why we might want to bring the duck back.
0:57
So let's try a classical inheritance type of typing experiment. And this is referred to as nominal typing.
1:06
So let's try a traditional classical inheritance way of thinking through types, aka nominal
1:13
typing as opposed to structural and see how well that works out for us. Now this is a bit of the follies of object oriented programming.
1:21
I actually like object oriented programming, but when you get too much of it, it's bad.
1:26
It's like saying I like salt, but you can't have salt for dinner. That's bad. So let's talk about vehicles. Okay.
1:33
So we have a vehicle and we want to categorize other types of things that drive around or, you know, vehicle like, so, well, motorcycles, motorcycles
1:44
are vehicles you can get on them. They'll move you from place to place. Sometimes they're fun.
1:49
Sometimes they're cold and wet, but you know, they're a vehicle.
1:53
Clearly, cars, cars are probably the very first thing you thought of when I said vehicle.
2:00
Let's think about some of the traits that these vehicles have. Well, cars and motorcycles have transmissions. Sure.
2:07
So our base class, our vehicle in its type definition by virtue of being a class, will
2:13
have to have a transmission field or properties or something like that. You'll have to have an engine, you know, brakes, throttle and keys.
2:21
That seems reasonable for what we're gonna model, right? In the beginning, our motorcycle and car, they were awesome.
2:27
But this was such a good idea that we're gonna keep going. We now have an electric motorcycle,
2:32
maybe one of those cool zero motorcycles that you can get. So sure, is it a motorcycle? You bet it's a motorcycle. It's an unusual one.
2:41
It's electric, it doesn't have gas, but other than that, I don't think a change in the engine makes a big difference. So yeah, motorcycle.
2:50
What about an off-road motorcycle, like a motocross bike, an enduro bike, even an adventure bike, maybe? Well, is that a motorcycle? Sure.
2:59
Not all motorcycles have to ride on the road. So yeah, this seems like it's working fine. However, the farther we go down this chain,
3:07
we start to run into things that don't really fit the constraints of the vehicle. Is an off-road motorcycle a vehicle? Sure.
3:17
Is an electric motorcycle a vehicle? Sure. But hey, the off-road bikes often don't have keys. Like the motocross bikes and enduro bikes,
3:25
the adventure ones probably do 'cause they go on the road and get parked in parking lots, but a motocross bike, no keys.
3:32
So what do we do about the key property inherent from the vehicle? That's the problem. Electric motorcycles probably don't have a transmission.
3:40
It's just connected straight up to the sprocket and it just goes. Maybe something, some gears to change how it actually,
3:49
like the rotational speed of the engine versus the sprocket, but it's not really a transmission in the traditional sense.
3:56
You don't shift it either automatically or manually, right? There's no transmission, let's say for this one.
4:03
All right, well, we're starting to push our luck, but we'll figure it out, right? What if we want a robot?
4:10
A robot that could move us around or something? You know, one of these crazy things, these Boston mechanic type things.
4:17
Well, you can drive it around and maybe some of them could get onto them. Is it a vehicle?
4:24
Well, nothing about the vehicle class that has to have a passenger. Maybe it's a vehicle. It's in like an autonomous robotic vehicle thing.
4:32
I don't know. But sure enough, we decided it's a vehicle. But it doesn't probably have a transmission.
4:38
If it has an engine, it probably has four of them as servos, one for each leg. Probably more than that.
4:44
I don't know if there's a throttle, maybe, maybe in this little computer brain, but who knows, right? You can just see this is really breaking down.
4:54
So two observations here. One object oriented programming doesn't solve all problems.
5:01
Most importantly, though, the further you try to push object oriented programming, the
5:05
deeper your inheritance hierarchies get, the less they match in the way they originally intended.
5:12
hear things like prefer composition over inheritance. With Python and
5:19
typing, we could have said, ""Hey, we have a function and it wants to do things and
5:24
so all it has to do is take a vehicle."" But then in order to match that typing
5:28
information, our robot, which probably could be used in a way that, you know,
5:32
things that drive around are, our robot has to inherit it from the vehicle just
5:38
to be using this type hierarchy so that it matches passes the constraint that the property
5:43
or field or whatever being passed in is actually a vehicle class. And that's a little bit crazy.
5:50
So remember our duck that we said goodbye to for a moment, our duck would have helped
5:54
out of here a lot because whatever the hierarchy is, it doesn't matter from a typing perspective.
6:00
If you could say, turn it on and make it go, regardless of which thing it is on the screen here, then duck typing would be exactly what you want.
6:08
So that's a little bit sad, huh?