9/11 Inheritance correct answer


#1

This is my solution to this assignment however assiging self.color etc for the ElectricCar class seems wrong. what is the correct way to do this?


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 %d MPG." % (self.color, self.model, self.mpg)
    def drive_car(self):
        self.condition = "used"
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("Ford", "Blue", 100, "molten salt")

#2

I came here for the same reason, although I think in this case as the method is

__init__

this is the only option, would love to know for sure


#3

I'm also stuck on this, and i'm confused by the instructions. They ask for an init() battery_type member variable, but I thought a member variable is outside of the the methods like condition is here:

class Car(object):
condition = "new"
def init(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg
def drive_car(self):
self.condition = "used"

def display_car(self):
    return "This is a %s" % str(self.color) + " " + str(self.model) + " " + "with %s" % str(self.mpg) + " " + "MPG."

class ElectricCar(Car):
def init(self, battery_type):
self.battery_type = battery_type

also it says to supply the model, color, and mpg in the childclass 'ElectricCar', but I thought the whole point of inheritance is that you were able to 'inherit' all of that information from the parent class 'Car'
Have I completely missed it?


#4

@wmahoney

It's simple if you remember some things.

Overriding the __init__ means that you cannot use that info unless you call it again.

Example: Calling the inherited classes __init__

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 %d MPG." % (self.color, self.model, 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

#5

Thanks! @zeziba
So in your example, 'battery_type' is an argument in the init() and then actually initialized below as self.battery_type = battery_type, this still counts as a member variable, within a method? for some reason I thought member variables were outside of methods, are they either, or is a variable outside of a method a global variable?


#6

Ok so none of that has been covered in this section. I am not impressed with codecademy. Nowhere was calling the init from the parent object mentioned in this lesson. I'm just supposed to magically figure it out? Or try to hunt for it or wait for an answer from someone on a forum? This is a beginner programming course, not forum navigation.


#7
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'
        return self.condition
class ElectricCar (Car):
    def __init__(self, battery_type):
        #super(ElectricCar, self).__init__(model, color, mpg)
        Car.__init__(model, color, mpg)
        self.battery_type = battery_type
my_car = Car('Farrari','silver',88)
my_car = ElectricCar('moltan salt')
print my_car.condition
print my_car.drive_car()

Can anyone run my code and tell me what's wrong with this code?


#8