Overriding methods


#1
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"
    
class ElectricCar(Car):
  def __init__(self, model, color, mpg, battery_type):
    super(ElectricCar, self).__init__(model, color, mpg)
    self.battery_type = battery_type
    def drive_car(self):
      self.condition = "like new"

my_car = ElectricCar("BMW", "Golden", 88, "molten salt")

print my_car.condition

my_car.drive_car()

print my_car.condition

I keep getting an error that I haven’t updated the condition to “like new”, instead it simply says “used”.
Any ideas on what went wrong here?


#2

In ElectricCar, you are overriding a method called drive_car(). Look at the indentation. What do you see?


#3

Oh I see it, thank you!


#4

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