Inheritance :Problem :(


#1


. 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):
        self.condition='used'

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

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


#2

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


#3

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.


#4

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:

super().__init__()

python will figure out the arguments needed :slight_smile:


#5

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