Inheritance :Problem :(


. my_car does not have a model member variable. :frowning:

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,self.mpg)
    def drive_car(self):

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

my_car = ElectricCar("DeLorean", "silver", 88,'molten salt')


you need to add model,color and mpg to self in init of ElectricCar


You can also add:
super(ElectricCar, self).init(model,color,mpg)

here you use your init from the Car class, this way you dont need to retype each variable.


is super() already covered? I would personally choice that approach is well, but i didn't want to introduce something new.

Just for for fun, in python3 you could do:


python will figure out the arguments needed :slight_smile:


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