Inheritance?

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")

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.

1 Like
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
2 Likes

You still have an error? So yes, what?

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

Thanks

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

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.