9. Inheritance - Unsure



I'm not getting any errors. My question is this - what's the point of Inheritance when I still have to type out the bits such as "self.model = model" ? Take a look at the code starting at "class ElectricCar(Car):" --- by inheriting Car(), why doesn't it actually inherit the code that I've already entered for Car()?

Sorry if this seems trivial or if I am missing the point, but I don't see the benefit of inheritance if I still have to tell the new class what everything means, when I already told its parent what it means.

class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg
    def display_car(self):
        return "This is a %s %s with %s MPG." %(self.color, self.model, str(self.mpg))
    def drive_car(self):
        self.condition = "used"
class ElectricCar(Car):
    def __init__(self, model, color, mpg, battery_type):
        self.model = model
        self.color = color
        self.mpg = mpg
        self.battery_type = battery_type
my_car = ElectricCar("Prius", "Gold", 0, "molten salt")

Replace this line with your code.


Hi @cluelist,

Since we are adding a battery_type instance variable when we create the ElectricCar class, we need to overwrite the original __init__ method, so that it initializes that instance variable in addition to the three others. Because we overwrote that method, it replaces the original that we wrote for Car, and we must provide for initializing the three other instance variables. We can explicitly initialize them as you did. However, we could instead reuse some of the work we did for the Car class.

We can call the original __init__ method to do part of the initialization work, as follows ...

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

The above uses the super function to call the original __init__ method. That initializes three of the instance variables, as in the Car class. Thereafter, we only need to initialize the battery_type instance variable.

See documentation at Built-in Functions: super.


When were we supposed to have absorbed that before now though?

This lesson caught me very off guard, in terms of how poorly worded the Instructions were.


Hi @mattcjeller,

The super function was presented in the Introduction to Classes: This Looks Like a Job For... exercise. You are not expected to use it in the current exercise, but it is an available option, if you want to use it for practice.

Yes, the inheritance material is a little more complicated than the previous material, and the instructions are not always as clear as you want them to be. This material does get easier with practice, though.


@appylpye thank you for the responses and clarification!


2 posts were split to a new topic: Inheritance Problem


2 posts were merged into an existing topic: Inheritance Problem