9. Inheritance


<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
Code correct but do’n understand what this line is : Car.__init__(self,model,color,mpg)
<What do you expect to happen instead?>

```python 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 " + self.color + " " + self.model + " " + "with " + str(self.mpg) + " MPG." def drive_car(self): self.condition = "used" my_car = Car("DeLorean", "silver", 88) print my_car.condition my_car.drive_car() print my_car.condition class ElectricCar(Car): def __init__(self,model,color,mpg,battery_type): Car.__init__(self,model,color,mpg) self.battery_type = battery_type my_car = ElectricCar("Toyota","silver",5,"molten salt")
<do not remove the three backticks above>

It is necessary to initialize the Car class instance so it has all the base attributes.


Ahhhhhhh.I thought i didn’t have to


Same. It appears to have been omitted from the instructions.


@mtf im sorry, i just have (or had) the same problem, i know that is necessary to add the
Car.__init__(self,model,color,mpg) but i still dont quite get it (i´ve read some stuff but i still got some doubts), like why is that necessary?.

for example, i thought that by calling just

    def __init__(self, battery_type)```

the ```ElectricCar``` would inherit all the parameters from ```Car``` and it would'nt be necessary to write those again, but it seems that not only we need to do that, but also we have to "call" the class as a function within out child class:

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

forgive the format of the previous code, i dont know how to

anyways, those are my questions, i apreciate any help :slight_smile:

I know this is confusing, and my own perspective is not exactly crystal clear. Let’s remove that line,

 # Car.__init__( ... )

Run the code, and this error results (or one similar)…

AttributeError: ‘ElectricCar’ object has no attribute ‘color’.

We have not defined the attribute in the base class as near as I can tell. We have two options, either the above line, or,

super(ElectricCar,self).__init__(model, color, mpg)

Will need to invite a more competent member to address this in precise terms.

i see, this is kinda confusing hehe

when you define a new init() in derived class, it is necessary to call into that class the init method of its superclass bc you have overridden it.
if you define a NewClass(Superclass) w/o defining init(), Superclass.init(args) remains the default init for the NewClass, and calling the Superclass.init(args) is not necessary if you want the args. of the init method to be requisites for instances you create.

maybe something that will help clarify:
if one wanted to create a NewClass(SuperClass) but not want to have to define all parameters required by the init method of the Superclass for each instance of NewClass, one would override these parameters by redefining init.


hi guys,i want to know is there any difference between

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


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

maybe except the second is more complicated.

The second example overrides the properties in the superclass. The first one inherits them, after having passed up the parameters to initialize them.


i see, thanks for the answer, this helps :slight_smile:

now that i’ve read the answers,basically these two do the same right?

super(ElectricCar,self).__init__(model, color, mpg) 

where the latter one calls the build-in function super to call the __init__ as well, right?

They are both the same in one regard, but super() is much more robust when it comes to multiple inheritence. Recommend chase down some reading on this topic, My explanation likely does no justice, by comparison.

alright then, thanks a lot for your answers man :smiley:

1 Like

yes,you’re right.Thank you, Roy.

1 Like