72

This question already has an answer here:

If I have a python class as:

class BaseClass(object):
#code and the init function of the base class

And then I define a child class such as:

class ChildClass(BaseClass):
#here I want to call the init function of the base class

If the init function of the base class takes some arguments that I am taking them as arguments of the child class's init function, how do I pass these arguments to the base class?

The code that I have written is:

class Car(object):
    condition = "new"

    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg

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

Where am I going wrong?


  • Your format is wrong, is that a copy-and-paste problem? Python will bark on wrong format. - Mingyu
  • @Mingyu format is wrong? You mean indentation or am I missing something else here? - Prakhar Mohan Srivastava
  • Yea.. I mean indentation. Please look at my answer below. - Mingyu

Linked


Related

Latest