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