Inheritance


#1



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


I pass the exercise but I still think there is a repetition in my code


less code than I have wrote dry : don't repeat yourself


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 = ElectricCar("VW", "white", 0, "molten salt")


#2

there is, to reduce your code you could use super() to call parent __init__() method, that would reduce your code.

you could also give a named argument to your init of Car:

class Car(object):
     def __init__(self, model, color, mpg, battery_type=None):

then ElectricCar doesn't need a init method, so Cars init method is called automatically.


9.Inheritance
9.Inheritance
#3

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