Python for .NET Developers Transcripts
Chapter: OOP: Object-Oriented Python
Lecture: Abstract cars
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
This refuel method, remember our assumption was the base car because we have electrics, gas, maybe diesel cars we have no idea how to fuel this car.
0:08
It's up to each individual car to figure out how it's fueled. Well, we're going to have some classes that derive from this, as you already saw.
0:16
But what we want to do is we want to make sure that you cannot explicitly create this, but you have to create a fully formed specialization of car.
0:24
In C# we saw that we could write something like this, like abstract car, right. We don't have this concept of public so maybe
0:34
we could write this, mm-mm, it's not looking like it, is it? So Python has a perfectly fine way to deal with this.
0:40
It's just not through keywords, it's through inheritance. So what we're going to do is we're going to use a module called ABC, so I'm going to say
0:48
import abc for Abstract Base Classes. And here we're going to say abc.ABC. Kind of wish the naming was a little more explicit there
0:59
but abstract base class. So this is cool. Down here we're going to say this refuel method is an @abc.abstractmethod.
1:10
This syntax here is new, I don't believe we've talked about it before, it should remind you a little bit of attributes.
1:16
It's called a decorator and it serves a lot of the same roles as an attribute does. It distinguishes the thing that has got the decorator on it
1:25
to make it behave differently or to indicate that it has different behavior but it's actually implemented super different
1:31
than typical attributes in C# are implemented as. Those are like compile-type things. This is actually something that runs an extra function
1:40
that modifies how these behave. We just put this on here and if we try to run this again it says, No, no, no, you cannot instantiate
1:48
an abstract car method, a class with the abstract method refuel. Alright, so that's done what we've achieved.
1:56
We can't create a car, now we have to create specializations that implement refuel.