Hi folks! I’m wondering why, when you inherit a parent class, you still have to include all the previous member variables. Here is the code I wrote, which passes:
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 " + self.color + " " + self.model + " with" + " " + str(self.mpg) + " 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 my_car = Car("DeLorean", "silver", 88) my_car = ElectricCar("Mazda 2", "blue", 34, "molten salt")
I thought I could basically append
battery_type to the
Car class variable. It threw an error, so I typed out all the previous variables. My question: if you still have to duplicate all the variables in the Parent class, why not just write a new class that is independent of the Parent?