9/11 Inheritance What is the problem?


#1


class Car(object):
    def drive_car(self):
        self.condition = 'used'
    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))
my_car = Car("DeLorean", "silver", 88)
print my_car.condition
print my_car.drive_car()
print my_car.condition


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


my_car = ElectricCar("molten salt","DeLorean", "silver", 88)

I get error:

Traceback (most recent call last):
  File "python", line 25, in <module>
  File "python", line 19, in __init__
TypeError: unbound method __init__() must be called with Car instance as first argument (got ElectricCar instance instead)

or Codecademy:

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


#2

Oh...It was a spelling error:P


#3

@l1palas,
You could read
+++ displaying an Instance of a class object
http://stackoverflow.com/questions/1436703/difference-between-str-and-repr-in-python/2626364#2626364
http://stackoverflow.com/questions/1535327/python-how-to-print-a-class-or-objects-of-class-using-print

and then use

class Car(object):
    #doc
    '''
    This is doc2
    '''
    def drive_car(self):
        self.condition = 'used'
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg
    #def __str__(self):
    #    return'This is a %s %s with %s MPG.' % (self.color, self.model, str(self.mpg))
    def __repr__(self):
        return "<%s instance at %r>" % (self.__class__.__name__, id(self))
    def dump(obj):
        for attr in dir(obj):
            print "obj.%s = %s" % (attr, getattr(obj, attr))

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

#4

Instancing an object requires assing parameters in the same order of it's constructor init

'''python

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("DeLorean", "silver", 88, "molten salt")