Inheritance - arguments given vs takes


#1




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


Class EletricCar(Car):
def init(self, model, color, mgp, battery_type):
Car.init(self, model, color, mgp)
self.battery_type = battery_type

The above isn't taught by codecademy but is posted by a user.

How is self an argument? If i get 4 or 5 argument, it seems to fail. I also removed condition because it does not mention it in task.


class Car(object):
    def __init__(self, model, color, mpg, condition):
        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"

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

my_car = EletricCar("Rama", "red", 99, "salt")


#2

first you have a problem here:

def __init__(self, model, color, mpg, condition)

condition shouldn't be part of init, it should be a member variable:

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

now see this stackoverflow question, we can use super() to do inheritance for us:

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

super() allows us to call parent methods.

and use mpg instead of mgp


#3

First, you need to define condition on line 2, right before the __init__ and it needs to be set to "new". This means that you don't need condition as a parameter.
Instead of

you need to use

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

super() lets you avoid referring to the base class explicitly, which can be nice. But the main advantage comes with multiple inheritance,
@stetim94 provided a good link.

Lastly,

"salt" needs to be "molten salt"


#4

Also note that EletricCar should be changed to ElectricCar throughout the program.


#5

The lessons don't say to keep or remove condition and so that's not an issue for ElectricCar. The spelling mistakes are minor and fixed, but still doesn't solve my issue.

The stackoverflow answer says "super() lets you avoid referring to the base class explicitly, which can be nice. But the main advantage comes with multiple inheritance, where all sorts of fun stuff can happen." (and links to another site). Avoiding and wrong code, to me, is two different things. In this exercise, we only have one inheritance, so why can I not use my method vs super()?

also super() method isn't said before this lesson and kind of seems like new python learners would be forced to read QA/stackoverflow/google to answer this correctly.

another edit:
this how to use super link has better answer than the link posted. Now I understand between super and unsuper, IMO the lesson needs to be updated to explain super and unsuper and define that it is asking for super.


#6

Hi, @ramasdf ,

Codecademy subjects your code to a submission correctness test (SCT) after you submit it. While there may be quite a variety of high quality alternate solutions to Codecademy exercises, the SCT can realistically be expected to accommodate only a subset of those potential solutions.

You can pass Exercise 9. Inheritance without using the super function, but we must expect there to be some limitations, due the the constraints of the SCT. However, you can copy your code and experiment all you want outside the venue of Codecademy.

The following solution, which avoids using super, is the closest that I was able to get to what you posted originally for the definition and instantiation of ElectricCar, and still have it pass the SCT ...

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

my_car = ElectricCar("Rama", "red", 99, "molten salt")

You can take this as a starting point, experiment from there, and perhaps get closer to the solution you hoped for than I did.


#7

Thanks, I understand the difference now. The suggestions made it sound as it super() is final answer SCT was looking. I did test out the differences on imy ide.

  • Rama

#8