Inheritance


#1

Could you please help with below code.

Error Code as follows
Traceback (most recent call last):
File "python", line 21, in
TypeError: init() takes exactly 2 arguments (5 given)

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"
         

my_car = Car("DeLorean", "silver", 88)
print my_car.condition
print my_car.drive_car()
print my_car.condition

class ElectricCar(Car):
    def __init__(self,battery_type):
        self.battery_type = battery_type
my_car = ElectricCar("molten salt","Tesla", "Red", 10)


#2

Your code is missing the line that instantiates the base class with standard parameters.

Inside the ElectricCar class, add this line after the __init__ method...

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

#3

2 posts were split to a new topic: Not been able to get this to work


#5

5 posts were split to a new topic: Does it also inherit if we don't create an init() in the child?


#8

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