Inheritance


#1



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


Traceback (most recent call last):
File "python", line 25, in
TypeError: init() takes exactly 2 arguments (5 given)


I have three questions:
1.Should I declare model, color and MPG again in the inherited class too?
2.In the order of passing the arguments, is battery_type the last?
3.In another post, Codeacademy clarified to me that ,'super' is used in cases where one particular method has to be both overridden and retained as defined in the parent class.
Should I use the super function in this case? If so, I do not understand why?


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,self.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,battery_type):
        self.battery_type=battery_type
        
my_Car =ElectricCar("Jaguar", "Black", 95, "molten salt")


#2

File "python", line 25, in 
TypeError: init() takes exactly 2 arguments (5 given)

Your init function need three more arguments.
def __init__(self,battery_type):

@makokam
hint**


#3

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,self.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,battery_type):
        self.model = model
        self.color = color
        self.mpg   = mpg
        self.battery_type=battery_type
        
my_Car =ElectricCar("Jaguar", "Black", 95, "molten salt")

For the above code, I am getting the following error.

Oops, try again. my_car does not appear to be an instance of ElectricCar.


#4

After you defined a new class (inheriting from Car)
You have to defined an instance of ElectricCar which is my_car

How we make an instance from ElelctricCar?
Revision...
suppose if I had to make an instance X using ElectricCar
I'd do this..
X = ElectricCar(values separated by comma)

Update**
@makokam kay you did that but you still not passed three variables that your init function needs there..
def __init__(self,battery_type):

Add model,color and mpg as function's arguments?

further hint**
If I had to add a new arguments..i'd do this..

def __init__(self,battery_type, z):
        self.battery_type=battery_type
        self.z = z

#5

Is there no need to use super function here?**


#6

It has!! because It(ElectricCar) gets many properties from upper class(It means that lower class can access properties and methods of upper Class)!

Inheritance makes a class more powerful and more organized!

@makokam
ps: delete your solution(final working code) from last post to avoid copy pasting of answer by others :slight_smile:


#7

I don't understand....

I am declaring the variables(color,model,mpg) again in the derived class( ElectricCar). Then what's the point of inheritance? :cry:


#8

Understand it like this...

suppose a guy have a chocolate factory.
Suppose he makes a type of chocolate which is very famous ,lets say its name is
X
some parameters of this chocolate..
sugar : 40gm
choco: 30 gm

so now this guy want to make a new chocoBar say Y of same ingredient but with a new flavour say he want to add milky taste..
so to do this he have to put these ingredients..
sugar: 40gm
choco: 30gm
new_Ingredient : somenumber gm

Can you see that when new chocoBar want to have old one (X)'s flavour ,to do this it must be made using same things(variables, sugar and choco) ?

do reply If it makes your doubt a bit clear or not!


#9

Yes I understand that aspect.

I dont understand how to test inheritance in this case.


#10

You'll get it real soon!I'd suggest to keep going ahead, The concepts aren't easy or hard but it needs time to have / create a working level understanding. :slight_smile:

I'm adding a simple article that may clear your doubts !
Further reading...
http://www.jesshamrick.com/2011/05/18/an-introduction-to-classes-and-inheritance-in-python/


#11

I do not understand if the code is working, because I have defined everything again under the derived class

or

if it is working because Electric Car actually derives from its parent class.


#12

if it is working because Electric Car actually derives from its parent class.

Ya, You're getting there! :slight_smile:
Just give that article a read!!
I hope it helps!


#13

Hi @makokam and @greentreemee I was stuck and your conversation helped a lot!

I will try and add to the answer @greentreemee gave to your question on the point of inheritance. Think about what you would do if you wanted to say, display the properties of "my_car" or print out the condition of "my_car". With inheritance you're able to do that with an object from "ElectricCar class" using the methods and variables inherited from "Car class". in this case you do not need to rewrite these methods all over again in "ElectricCar class".

All you do is:


my_car.display_car() or

print my_car.condition


The highlighted are the items inherited (from Car) and reused in your child class(ElectricCar).

I hope that helps!!


#14

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