8 Ways to Call a Parent Constructor in Python
An electric car has a brand and a color, just like any vehicle. It also has a battery. With inheritance, Vehicle.__init__() can initialize the shared attributes while ElectricCar.__init__() initializes the battery.
Each example below is a complete Python program you can copy and run on its own.
How super().__init__() works
Inside an instance method, super() lets you call the next implementation in Python’s method resolution order. In these examples, that implementation is Vehicle.__init__(). Python supplies the current object as self, so you pass only the values the parent needs.
super().__init__(brand, color) uses the local variables received by the child constructor. When the parent runs self.brand = brand, it stores that value on the same object that the child uses for self.battery.
1. Pass positional arguments
Call syntax super().__init__(brand, color)
The first value goes to the parent's brand parameter. The second goes to color.
class Vehicle:
def __init__(self, brand, color="white"):
self.brand = brand
self.color = color
class ElectricCar(Vehicle):
def __init__(self, brand, color, battery):
super().__init__(brand, color)
self.battery = battery
car = ElectricCar("Toyota", "red", "75 kWh")
print(car.brand, car.color, car.battery)
Output
Toyota red 75 kWh
2. Pass keyword arguments
Call syntax super().__init__(color=color, brand=brand)
Python matches the parent's parameters by name. In brand=brand, the left side names the parent's parameter and the right side reads the child's local variable.
class Vehicle:
def __init__(self, brand, color="white"):
self.brand = brand
self.color = color
class ElectricCar(Vehicle):
def __init__(self, brand, color, battery):
super().__init__(color=color, brand=brand)
self.battery = battery
car = ElectricCar("Toyota", "red", "75 kWh")
print(car.brand, car.color, car.battery)
Output
Toyota red 75 kWh
3. Mix positional and keyword arguments
Call syntax super().__init__(brand, color=color)
The first value is positional. The color is passed by name.
class Vehicle:
def __init__(self, brand, color="white"):
self.brand = brand
self.color = color
class ElectricCar(Vehicle):
def __init__(self, brand, color, battery):
super().__init__(brand, color=color)
self.battery = battery
car = ElectricCar("Toyota", "red", "75 kWh")
print(car.brand, car.color, car.battery)
Output
Toyota red 75 kWh
4. Use the parent’s default value
Call syntax super().__init__(brand)
The child supplies only brand. The parent uses its default color of "white".
class Vehicle:
def __init__(self, brand, color="white"):
self.brand = brand
self.color = color
class ElectricCar(Vehicle):
def __init__(self, brand, battery):
super().__init__(brand)
self.battery = battery
car = ElectricCar("Toyota", "75 kWh")
print(car.brand, car.color, car.battery)
Output
Toyota white 75 kWh
5. Unpack a tuple into positional arguments
Call syntax super().__init__(*vehicle_args)
The * expands the tuple into two positional arguments in tuple order.
class Vehicle:
def __init__(self, brand, color="white"):
self.brand = brand
self.color = color
class ElectricCar(Vehicle):
def __init__(self, brand, color, battery):
vehicle_args = (brand, color)
super().__init__(*vehicle_args)
self.battery = battery
car = ElectricCar("Toyota", "red", "75 kWh")
print(car.brand, car.color, car.battery)
Output
Toyota red 75 kWh
6. Unpack a dictionary into keyword arguments
Call syntax super().__init__(**vehicle_kwargs)
The ** expands the dictionary into keyword arguments. Its keys match the parent’s parameter names.
class Vehicle:
def __init__(self, brand, color="white"):
self.brand = brand
self.color = color
class ElectricCar(Vehicle):
def __init__(self, brand, color, battery):
vehicle_kwargs = {"brand": brand, "color": color}
super().__init__(**vehicle_kwargs)
self.battery = battery
car = ElectricCar("Toyota", "red", "75 kWh")
print(car.brand, car.color, car.battery)
Output
Toyota red 75 kWh
7. Forward *args and **kwargs
Call syntax super().__init__(*args, **kwargs)
The child collects args = ("Toyota",) and kwargs = {"color": "red"}. The parent receives "Toyota" and color="red". Since battery follows *args in the signature, callers pass it by keyword. The child keeps that value for itself.
class Vehicle:
def __init__(self, brand, color="white"):
self.brand = brand
self.color = color
class ElectricCar(Vehicle):
def __init__(self, *args, battery, **kwargs):
super().__init__(*args, **kwargs)
self.battery = battery
car = ElectricCar("Toyota", color="red", battery="75 kWh")
print(car.brand, car.color, car.battery)
Output
Toyota red 75 kWh
8. Call the parent class directly
Call syntax Vehicle.__init__(self, brand, color)
Calling Vehicle.__init__ through the class requires you to pass self explicitly. This selects Vehicle directly. super() follows Python’s method resolution order, which matters when multiple classes cooperate through inheritance.
class Vehicle:
def __init__(self, brand, color="white"):
self.brand = brand
self.color = color
class ElectricCar(Vehicle):
def __init__(self, brand, color, battery):
Vehicle.__init__(self, brand, color)
self.battery = battery
car = ElectricCar("Toyota", "red", "75 kWh")
print(car.brand, car.color, car.battery)
Output
Toyota red 75 kWh
Why super().__init__(self.brand) can raise AttributeError
Python evaluates self.brand before it calls the parent initializer. In these examples, the parent creates that attribute, so it does not exist yet when the child starts. Pass the local parameter brand, which already holds the input value.
If the child has already assigned self.brand, then passing self.brand works. Here, passing brand lets the parent handle the assignment once.
Which form should you use?
| Situation | Form |
|---|---|
| The child has a small, known set of parent arguments | super().__init__(brand, color) |
| You want the meaning of each argument visible at the call site | super().__init__(brand=brand, color=color) |
| The parent’s default is what you want | super().__init__(brand) |
| The values are already in a tuple or dictionary |
super().__init__(*vehicle_args) or super().__init__(**vehicle_kwargs)
|
| The child accepts and forwards extra arguments | super().__init__(*args, **kwargs) |
| You deliberately need to select a specific base class | Vehicle.__init__(self, brand, color) |
Top comments (0)