11/11 Inheritance


#1

https://www.codecademy.com/courses/python-intermediate-en-egNXj/1/3?curriculum_id=4f89dab3d788890003000096

I am stuck on Inheritance in the Introduction to Classes section. My understanding of inheritance is that I should be able to create a new class (in this case ElectricCar) that inherits from Car. In all of the previous examples and FAQ topics I've read it looks like I have to retype all of the information from the parent class, Car. Is there a way of modifying the parent class without having to retype all of the initial information?

I've added my own code that could also use some help figuring out.

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"
        return self.condition


class ElectricCar(Car):
    def __init__(battery_type):
        Car.__init__(self, model, color, mpg, battery_type)
        self.battery_type = battery_type
        
my_car = ElectricCar("prius", "red", 99, "molten salt")

#2

console error:

Traceback (most recent call last):
File "python", line 21, in
TypeError: init() takes exactly 1 argument (5 given)

Unfortunately I don't have enough background knowledge to criticize this line:

Car.__init__(self, model, color, mpg, battery_type)

Following the instructions with this works as expected:

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

#3

You probably don't want to modify the parent itself. If you want the same behavior for some method as the parent, then don't override that method.
If you want to do what the parent does and more, then call the parents' version through super and then do the additional stuff.
If you want to do something different from the parents, then it isn't repeating the code.

An electric car is a car, so it makes sense for it to do all the things that a car does. (there's the mpg attribute though, that's kinda out of place..)