Python for the .NET developer Transcripts
Chapter: OOP: Object-Oriented Python
Lecture: Base constructors and super()
0:00 In our C# version, we saw that
0:02 for the electric cars
0:03 we didn't have to indicate that they were electric
0:05 nor did we have to say the number of cylinders
0:08 which is always going to be zero.
0:10 We did that by changing the constructor.
0:14 If we go over to this
0:15 we can say we want to define a special constructor.
0:20 And it was going to take
0:21 let me just rob a little bit from here
0:22 model name and that is going to be similar.
0:29 Like so. And we can just do a quick little pass for a second
0:32 because I want you to see some of the issues
0:34 or the help that the editor is going to give us.
0:37 So in C# what we did was we said base.
0:41 We put stuff here and then we had curly braces like that.
0:45 Well the colons already used so we're not
0:46 going to do that.
0:47 And what we do down here is we can actually say
0:49 super and get to the base class or super class
0:53 which is car. We can call it constructor explicitly.
0:57 That's how you do it.
0:58 And then what goes in here?
0:59 Well the model name goes in there.
1:01 And then the engine type is electric.
1:04 The number of cylinders is zero.
1:06 And the base price is like that.
1:08 So here's how we accomplish the same thing in Python.
1:11 Now if you try to run it where we create the electric cars
1:14 that part's going to get unhappy.
1:16 As you can see I thought we had 3 arguments
1:19 but 5 were given.
1:20 By the way, notice 3 and 5 not 4 and 2
1:24 the way you probably think about it.
1:25 But let's jump over here where we're using it.
1:29 It's part of that self thing.
1:30 So it says we got some two extra things here.
1:34 And the two extra things are the electric
1:35 and the number of cylinders.
1:37 Both drop that and that. Run it again.
1:41 Works like a charm. Right so go back here.
1:45 The way we delegate to the base class
1:49 or super class constructor, initializer in Python
1:53 is we call super and then the initializer explicitly.
1:56 This should always be first. Then other stuff, right.
2:03 You want it to do all the set up and then
2:05 make additional changes after that I would suspect
2:07 most of the time.
2:08 So I guess I'll leave those comments in here for you.
2:11 This is how you do it.
2:12 This is how you call the base constructor
2:13 and you can have specializations.
2:15 Notice the other ones just don't even define
2:17 initializer and they just fall through to the car's
2:20 definition of initialize.