9. Inheritance


#1



Code correct but do'n understand what this line is : Car.__init__(self,model,color,mpg)


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"
my_car = Car("DeLorean", "silver", 88)
print my_car.condition
my_car.drive_car()
print my_car.condition
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("Toyota","silver",5,"molten salt")


#2

It is necessary to initialize the Car class instance so it has all the base attributes.


#3

Ahhhhhhh.I thought i didn't have to


#4

Same. It appears to have been omitted from the instructions.


#5

@mtf im sorry, i just have (or had) the same problem, i know that is necessary to add the
Car.__init__(self,model,color,mpg) but i still dont quite get it (i´ve read some stuff but i still got some doubts), like why is that necessary?.

for example, i thought that by calling just
class ElectricCar(Car):
def __init__(self, battery_type)

the ElectricCar would inherit all the parameters from Car and it would'nt be necessary to write those again, but it seems that not only we need to do that, but also we have to "call" the class as a function within out child class:

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

forgive the format of the previous code, i dont know how to

anyways, those are my questions, i apreciate any help :slight_smile:


#6

I know this is confusing, and my own perspective is not exactly crystal clear. Let's remove that line,

 # Car.__init__( ... )

Run the code, and this error results (or one similar)...

AttributeError: 'ElectricCar' object has no attribute 'color'.

We have not defined the attribute in the base class as near as I can tell. We have two options, either the above line, or,

super(ElectricCar,self).__init__(model, color, mpg)

Will need to invite a more competent member to address this in precise terms.


#7

i see, this is kinda confusing hehe


#8

when you define a new init() in derived class, it is necessary to call into that class the init method of its superclass bc you have overridden it.
if you define a NewClass(Superclass) w/o defining init(), Superclass.init(args) remains the default init for the NewClass, and calling the Superclass.init(args) is not necessary if you want the args. of the init method to be requisites for instances you create.

maybe something that will help clarify:
if one wanted to create a NewClass(SuperClass) but not want to have to define all parameters required by the init method of the Superclass for each instance of NewClass, one would override these parameters by redefining init.


#9

hi guys,i want to know is there any difference between

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

...
and

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

...
maybe except the second is more complicated.


#10

The second example overrides the properties in the superclass. The first one inherits them, after having passed up the parameters to initialize them.


#11

i see, thanks for the answer, this helps :slight_smile:


#12

now that i've read the answers,basically these two do the same right?

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

super(ElectricCar,self).__init__(model, color, mpg)

where the latter one calls the build-in function super to call the __init__ as well, right?


#13

They are both the same in one regard, but super() is much more robust when it comes to multiple inheritence. Recommend chase down some reading on this topic, My explanation likely does no justice, by comparison.


#14

alright then, thanks a lot for your answers man :smiley:


#15

yes,you're right.Thank you, Roy.