What Does Super().__Init__(*Args, **Kwargs) Do in Python?
Last Updated :
19 Mar, 2024
In Python, super().__init__(*args, **kwargs) is like asking the parent class to set itself up before adding specific details in the child class. It ensures that when creating an object of the child class, both the parent and child class attributes are initialized correctly. It’s a way of saying, In this article, we will see about super().__init__() in Python.
Advantage of super()_init_(*Args, **kwargs)
- Inheritance: When you create a new class (subclass) that inherits from an existing class (superclass), you can reuse the methods and attributes of the existing class. This means you get the benefits of what the parent class has, and you can also customize it.
- Super() Built-in: A proxy object is like a middleman that ensures the correct order when a class inherits from multiple classes. It makes sure that when you call a method, it finds and uses the right method in the right order, respecting the hierarchy of classes.
- Constructor Method: When you create a new thing (object) in Python, __init__() is a special method that gets it ready by setting up its initial characteristics or qualities.
- Positional Arguments (*args): Passed to the constructor without names.
- Keyword Arguments (**kwargs): Passed to the constructor with names (key-value pairs).
What Does Super().__Init__() Do in Python
Below are some ways of to understand about Super().__Init__(*Args, **Kwargs) in Python:
Single inheritance with super().__Init__() Function
In this example, in below code the line `super().__init__(name)` in the `Child` class invokes the parent class constructor, initializing the shared attribute `name`. This ensures proper inheritance. The print statements then display the values for both parent and child instances.
Python3
class Parent:
def __init__(self, name):
self.name = name
class Child(Parent):
def __init__(self, name, age):
super().__init__(name) # Call parent's constructor first
self.age = age
parent = Parent("Alice")
child = Child("Bob", 12)
print("Parent:", parent.name)
print("Child:", child.name, child.age)
OutputParent: Alice
Child: Bob 12
Multiple Inheritance with super().__Init__() Function
In this example, in below code Derived
class inherits from Base1
and Base2
. Its constructor uses super()
to initialize attributes from Base1
and explicitly calls Base2.__init__(self, y)
to set the attribute from Base2
. The print statement displays the values for x
, y
, and z
in the Derived
instance.
Python3
class Base1:
def __init__(self, x):
self.x = x
class Base2:
def __init__(self, y):
self.y = y
class Derived(Base1, Base2):
def __init__(self, x, y, z):
super().__init__(x) # Call first base class constructor
# Call second base class constructor explicitly
Base2.__init__(self, y)
self.z = z
derived = Derived(1, 2, 3)
print("Derived:", derived.x, derived.y, derived.z)
Multi-Level Inheritance with super().__Init__() Function
In this example, in below code Child
class inherits from Parent
, which in turn inherits from Grandparent
. The constructors are called in a chain: Grandparent
initializes name
, Parent
adds age
, and Child
includes hobby
. The print statement displays the values for name
, age
, and hobby
in the Child
instance.
Python3
class Grandparent:
def __init__(self, name):
self.name = name
class Parent(Grandparent):
def __init__(self, name, age):
super().__init__(name) # Call grandparent's constructor
self.age = age
class Child(Parent):
def __init__(self, name, age, hobby):
super().__init__(name, age) # Call parent's constructor
self.hobby = hobby
child = Child("Charlie", 8, "reading")
print("Child:", child.name, child.age, child.hobby)
OutputChild: Charlie 8 reading
Another Example
In this example, super().__init__(name) is used in the Child class to call the constructor of the Parent class. This ensures that the initialization code in the Parent class is executed before the specific initialization code in the Child class.
Python3
class Parent:
def __init__(self, name):
self.name = name
print(f"Parent class initialized with name: {self.name}")
class Child(Parent):
def __init__(self, name, age):
super().__init__(name)
self.age = age
print(
f"Child class initialized with name: {self.name} and age: {self.age}")
# Creating an instance of Child
child_instance = Child("John", 10)
OutputParent class initialized with name: John
Child class initialized with name: John and age: 10
Conclusion
In conclusion , super().__init__(*args, **kwargs) is like asking for help from the parent class when creating a new object in the child class. It ensures that both the parent and child classes’ setup codes are carried out correctly, guaranteeing that everything starts off just right. It’s a crucial tool when you have a family of classes, making sure each member, whether parent or child, gets properly initialized.
Share your thoughts in the comments
Please Login to comment...