9/11 python is testing me


Hello and nice meeting you all. This is my first post !
I stormed through the codeacademy tutorial of python and i really enjoy and understand it ,then Classes happened.
I could use a little help with this:

Here is the code in text:

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 "+self.color+" "+self.model+" with "+str(self.mpg)+" MPG."
    def drive_car(self):

class ElectricCar(Car):
    def __init__(self,battery_type):
        Car.__init__(self, model, color, mpg)
my_car=ElectricCar('Tesla','blue',1,'molten salt')

Please explain to me what's wrong with it and why doesn't it work.

Thanks in advance!


@dariusfx Hey there,
Car.init isn't eligible. Delete that, and put with the parameters with that included with the correct initialization method. Like
def init(self, battery_type, model, color, mpg):
(and then the self.battery_type.... so on!)
Hope this helps! :smiley:


isn't the new init condition is supposed to be overwritten??

"However, we can define our own init() function inside the class, overwriting the default version. "

found this in some earlier tutorial (5. Initializing the Class)


Hi there!

The problem is here:

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

You want to use model, color, mpg from Parent class, but you did not pass them in the init of Child class.

It should be something like this:

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

Try it!


Why are you doing init twice in there? Doesn't make any sense

This is correct:

`class ElectricCar(Car):

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

my_car = ElectricCar("molten salt", "audi", "orange", 88)`


First you should define a member variable named battery_type.
It is belong to the ElectricCar Class. Then give value to it
Second, the line Car.__init__ is not correct.You can't apply "Car" directly,you'd better override the init mothed.


Actually, the init twice does make sense.

The whole point of having sub-classes inherit attributes of their parent classes is to avoid having to redefine everything in the sub-class.

Why would someone redo four lines of code redefining what was already defined in the parent class when they can do it in one by initializing those attributes?

I know four lines of code seems piddly here, but in large constructs with multiple classes, sub-classes and functions...that can bloat your software a great deal.

as stated by ainomc, this:

def init(self, model, color, mpg, battery_type):
-------Car.init(self, model, color, mpg)

works great (Formatting is non-cooperative it seems)


Thank you! I was trying to figure this out but didn't know how to pass those to the child class. Now it makes sense!