Profile image of hommedulait
Submitted by hommedulait
over 13 years

How does Inheritance work with __init__( )?

When we define a class, like:

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

We might initialize with additional input variables such as color in the example above.

Then, say we define a derived class, like

class ElectricCar(Car):
    def __init__(self, color, typeOfBattery):
     self.color = color
     self.typeOfBattery = typeOfBattery

The new class (ElectricCar) inherits the member variables (condition), and inherits the methods (paintCar). However, it seems we need to include all of the ‘initial’ variables (color) again in the definition of the new class if we want to add a new possible input variable (like typeOfBattery). Is there any way to automatically include these variables in the definition/initialization of the new class, while introducing new variables, without having to re-type the init( ) method to include all of the parent variables? It seems like this would become rather cumbersome for large hierarchies of classes.

Answer 50f0cf639699cd362e000b5e

6 votes

Permalink

You can use super(ElectricCar, self).__init__(color) inside of ElectricCar‘s __init__() function.

Detailed information in official Python documentation.

Profile image of sx
Submitted by sx
over 13 years

5 comments

Profile image of fahyij
Submitted by fahyij
about 13 years

Does it mean ,I still have to re-type the variable,such as color in the parenthesis after the __init__

Profile image of fahyij
Submitted by fahyij
about 13 years

how wish I could just add “typeOfBatery” like this: def init(self,typeOfBatery): #without re-type color super(xxx,self).init() #inherit all the variables defined in parent class but ,it seems to be impossible.

Profile image of Kedlaw
Submitted by Kedlaw
about 13 years

but if we do that how many argument will child.init() expect ??

Profile image of sx
Submitted by sx
about 13 years

Sorry, I was out of town for a while (without internet). @fahyij yes you could, except the fact that you would be explicitly specifying the color variable like this: super(xxx, self).init(“black”). In the Car class, the color is not defined, it is a parameter of the init method, so you have to specify the color in either way

Profile image of sx
Submitted by sx
about 13 years

@Waldemar Bus If you have def init(self, typeOfBattery):, then child.init() would expect one variable. (Assuming that your child is ElectricCar class). So, newCar = ElectricCar(“battery”)

Answer 514b48d11bbddd4eb9000967

2 votes

Permalink

I thought the same thing, but, like you, it just felt wrong. Repetition is usually a sign that you’re not doing something in the most efficient way. I found this link which resulted in the following code which seems to work.

class ElectricCar(Car):
def __init__(self, model, color, mpg, typeOfBattery):
    Car.__init__(self, model, color, mpg)
    self.typeOfBattery = typeOfBattery
Profile image of ajaxWhiz49968
Submitted by ajaxWhiz49968
about 13 years

Popular free courses

  • In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.
    • Beginner Friendly.
      4 Lessons
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      11 Lessons
  • Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.
    • Beginner Friendly.
      6 Lessons
Explore full catalog