Inheritance?


#1

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

my_car = ElectricCar("Rama", "red", 99, "molten salt")

#2

you have two options here, either add model color and mpg to self again:

self.model = model
# same for color and mpg

or you can use super() to call the init method of the parent class

what you are doing is unfortunately not possible.


#3

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

#4

You still have an error? So yes, what?


#5

Hi! @stetim94
I didn't understand how we can use super().
Could you explain please with an example.

Thanks


#6

Did you check other forum topics and documentation on how the super() function works? It allows for calling parents methods


#7

class C(B):
    def method(self, arg):
        super(C, self).method(arg)

Look here https://docs.python.org/2/library/functions.html#super

It's can be some confusing that the first parameter in super is C, but remember - super gets the base class of C class.