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
You can use super(ElectricCar, self).__init__(color) inside of ElectricCar‘s __init__() function.
Detailed information in official Python documentation.
5 comments
Does it mean ,I still have to re-type the variable,such as color in the parenthesis after the __init__
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.
but if we do that how many argument will child.init() expect ??
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
@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
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
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