To me this seems like the lesson for which CodeAcademy has done the worst job preparing us to understand what’s going on.
It took me a while to figure out that I had to reinitialize all the values I already initialized in the Car class in the ElectricCar class and include the Car.init(self, model, color, mpg) line. I was expecting the ElectricCar class to inherit these arguments from the Car class and that reinitializing them wouldn’t be necessary. So you could initialize ElectricCar with battery_type and it would understand that this argument was to be appended to the existing arguments that exist within the Car class and when you create an ElectricCar you’d be able to pass all the arguments initialized in Car as well as the battery_type. This does not seem to be the case.
So, my question is - does inheritance only pass down methods and variables you’ve set within it? By that I mean that any arguments you want to pass in during the creation of an object of that class has to be initialized in every child class?
Apologies if I’ve made terminology mistakes in this post - more than happy to hear any corrections.
For those struggling to get it to work here’s my code that CodeAcademy allowed to pass
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):
Car.__init__(self, model, color, mpg)
self.battery_type = battery_type
my_car = ElectricCar("Tesla", "blue", 250, "molten salt")