How to call the base class __init__ method from a child class?

If I have a python class like:

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

And then I define a child class, for example:

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

If the init function of the base class accepts some arguments that I accept as arguments to the init function for the child class, how do I pass these arguments to the base class?

Code I wrote:

 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?

+53
python inheritance constructor
06 Oct '13 at 6:01
source share
4 answers

You can use super(ChildClass, self).__init__()

 class BaseClass(object): def __init__(self, *args, **kwargs): pass class ChildClass(BaseClass): def __init__(self, *args, **kwargs): super(ChildClass, self).__init__(*args, **kwargs) 

The fingerprint is incorrect, the code is changed here:

 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) car = ElectricCar('battery', 'ford', 'golden', 10) print car.__dict__ 

Here's the conclusion:

 {'color': 'golden', 'mpg': 10, 'model': 'ford', 'battery_type': 'battery'} 
+67
Oct 06 '13 at 6:06 on
source share
— -

As Minho noted, there is a problem with formatting. In addition, I strongly recommend that you do not use the Derived class name when calling super() , since it makes your code inflexible (code maintenance and inheritance issues). In Python 3, use super().__init__ . Here is the code after including these changes:

 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().__init__(model, color, mpg) 

Thanks to Erwin Mayer for pointing out a problem using __class__ using the super () function

+16
Jan 02 '15 at 10:17
source share

You can call a superclass constructor like this

 class A(object): def __init__(self, number): print "parent", number class B(A): def __init__(self): super(B, self).__init__(5) b = B() 

Note:

This will only work when the parent class inherits object

+7
Oct 06 '13 at 6:06 on
source share

If you are using Python 3, it is recommended that you simply call super () without any arguments:

 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().__init__(model, color, mpg) car = ElectricCar('battery', 'ford', 'golden', 10) print car.__dict__ 

Do not call super with the class , as this can lead to infinite recursion exceptions according to this answer .

+4
Feb 12 '16 at 7:36
source share



All Articles