Python for .NET Developers Transcripts
Chapter: OOP: Object-Oriented Python
Lecture: Base constructors and super()

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In our C# version, we saw that for the electric cars we didn't have to indicate that they were electric nor did we have to say the number of cylinders
0:09 which is always going to be zero. We did that by changing the constructor. If we go over to this we can say we want to define a special constructor.
0:21 And it was going to take let me just rob a little bit from here model name and that is going to be similar.
0:30 Like so. And we can just do a quick little pass for a second because I want you to see some of the issues
0:35 or the help that the editor is going to give us. So in C# what we did was we said base. We put stuff here and then we had curly braces like that.
0:46 Well the colons already used so we're not going to do that. And what we do down here is we can actually say
0:50 super and get to the base class or super class which is car. We can call it constructor explicitly. That's how you do it. And then what goes in here?
1:00 Well the model name goes in there. And then the engine type is electric. The number of cylinders is zero. And the base price is like that.
1:09 So here's how we accomplish the same thing in Python. Now if you try to run it where we create the electric cars that part's going to get unhappy.
1:17 As you can see I thought we had 3 arguments but 5 were given. By the way, notice 3 and 5 not 4 and 2 the way you probably think about it.
1:26 But let's jump over here where we're using it. It's part of that self thing. So it says we got some two extra things here.
1:35 And the two extra things are the electric and the number of cylinders. Both drop that and that. Run it again.
1:42 Works like a charm. Right so go back here. The way we delegate to the base class or super class constructor, initializer in Python
1:54 is we call super and then the initializer explicitly. This should always be first. Then other stuff, right. You want it to do all the set up and then
2:06 make additional changes after that I would suspect most of the time. So I guess I'll leave those comments in here for you. This is how you do it.
2:13 This is how you call the base constructor and you can have specializations. Notice the other ones just don't even define
2:18 initializer and they just fall through to the car's definition of initialize.


Talk Python's Mastodon Michael Kennedy's Mastodon