9. Inheritance


#1



Unless I'm missing something obvious (could well be the case), for the third line in the code below in the Inheritance exercise, the instructions do not state at any stage this syntax or its purpose, and unless one has worked with Python or other languages in the past, they won't get it. Of course once a student gets stumped enough to open the forums the answer is pretty evident, but it would be great to have this included in the lesson. Instructions below for reference:

"Create a class ElectricCar that inherits from Car. Give your new class
 an __init__() method of that includes a "battery_type" member variable in
 addition to the model, color and mpg.

Then, create an electric car named "my_car" with a "molten salt" battery_type. 
Supply values of your choice for the other three inputs (model, color and mpg)."

And the hint:

Redefining a method of a "child" class is as simple as including
a definition for that function inside the "child" class; this version will 
take precedence over the inherited version.
Remember to include the self keyword as the first input when you define
the __init__() method!


class ElectricCar(Car):
    def __init__(self, model, color, mpg, battery_type):
        Car.__init__(self, model, color, mpg)
        self.battery_type = battery_type


#2

Agreed. The lesson needs to make this a bit more clear. In seeing the solution, I completely understand why it would be needed, but I did not arrive at that in the lesson itself - had to come here to find it.


#3

Me as well. I assumed that a new class that inherits from a parent class automatically inherits the init method of the parent class.


#4

I had the exact same reasoning.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.