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 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): self.model = model self.color = color self.mpg = mpg self.battery_type = battery_type def drive_car(self): self.condition = "like new" my_car = ElectricCar("DeLorean", "silver", 88, "molten salt") print my_car.condition my_car.drive_car() print my_car.condition
This code prints out
new like new
my_car variable refers to the ElectricCar class, but it still prints out
new, which is the assignment to the variable
condition in the parent Car class.
If my_car is already referring to the ElectricCar class, why does it still prints the word
I hope I’m being clear. It’s hard to try and explain code questions