Python, 9/11, Inheritance, why doesn't this work?


#1

Why doesn't this work?

class Car(object):
    condition = "new"
    def _ _ init _ _(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg
          
    def display_car(self):
        print "This is a %s %s with %s" %s(self.color, self.model, str(self.mpg))
          
    def drive_car(self):
        self.condition = "used"
          
class ElectricCar(Car):
    def __init__(self, battery_type):
        self.battery_type = battery_type

my_car = ElectricCar("Agera", "red", "200", "molten salt")

I get the error in the box:
Traceback (most recent call last):
File "python", line 18, in
TypeError: init() takes exactly 2 arguments (5 given)

And the hint at the bottom:

Oops, try again. my_car does not appear to be an instance of ElectricCar.

I have looked for solutions on the forum, but no one appears to have one yet that doesn't involve simply duplicating all the info in the Cars class, in the ElectricCars class. This appears to me to be the wrong way to do it, as, what's the point of inheritance if you have to duplicate everything anyway? (and yes I know the indenting is off in the example above, for some reason the quotes kill the indenting)


#2

from the instructions:
Create a class ElectricCar that inherits from Car. Give your new class an init() method of that includes a "battery_type" member variable in addition to the model, color and mpg.

your init contains only self and battery type, not model, color and mpg, while init should include this


#3

What is the purpose of inheritance then? If I have to completely recreate the "car" class within ElectricCar, then inheritance isn't actually inheriting anything. Is this a bug, because this makes no functional sense.


#4

There are many great books out there that go over exactly what this is. I am going to suggest that you Learn Python The Hard Way.


#5

I don't think that the context of this lesson is very accurate. When making a child class you are inheriting the parents methods. The variables being passed as parameters will very on what you want to accomplish with the child class, so they have to be declared. In this case they are the same and it seams to be redundant.


#6

THANK YOU!
I've stopped with CodeCademy. To many bugs, to little explanation of what's going on. I'm ending up more confused than informed as the lessons get more complex


#7

dude,you type:
def _ _ init _ _(self, model, color, mpg):
instead of
def __ init __(self, model, color, mpg):


#8