Open In App

Python Super() With __Init__() Method

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In object-oriented programming, inheritance plays a crucial role in creating a hierarchy of classes. Python, being an object-oriented language, provides a built-in function called super() that allows a child class to refer to its parent class. When it comes to initializing instances of classes, the __init__() method is often used. Combining super() with __init__() can be particularly useful when you want to extend the behavior of the parent class’s constructor while maintaining its functionality.

What is super() With __init__() Methods?

The super() function in Python is used to refer to the parent class. When used in conjunction with the __init__() method, it allows the child class to invoke the constructor of its parent class. This is especially useful when you want to add functionality to the child class’s constructor without completely overriding the parent class’s constructor.

Syntax:

class ChildClass(ParentClass):

def __init__(self, *args, **kwargs):

super().__init__(*args, **kwargs)

# Child class specific initialization code

Here, super().__init__(*args, **kwargs) calls the __init__() method of the parent class with the arguments and keyword arguments passed to the child class’s constructor.

Python Super() With __Init__() Methods Example

Below, are the example of Python Super() With __Init__() Methods in Python:

Example 1: Basic Inheritance

In this example, below Python code defines two classes, `Animal` and `Dog`, with the `Dog` class inheriting from `Animal`. The `__init__` method in each class initializes attributes (`name` in `Animal` and `name` and `breed` in `Dog`). An instance of the `Dog` class named “Buddy” with the breed “Labrador” is created, and its attributes are printed, displaying the dog’s name and breed.

Python3




class Animal:
    def __init__(self, name):
        self.name = name
 
class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name)
        self.breed = breed
 
# Creating an instance of the Dog class
my_dog = Dog("Buddy", "Labrador")
print(f"My dog's name is {my_dog.name} and it's a {my_dog.breed}.")


Output

My dog's name is Buddy and it's a Labrador.

Example 2: Multiple Inheritance

In this example, below Python code defines three classes, `A`, `B`, and `C`. Class `C` inherits from both `A` and `B`. In the `__init__` method of class `C`, `super().__init__(a)` is used to call the constructor of class `A` with the parameter `a`, and `B.__init__(self, b)` is used to call the constructor of class `B` with the parameter `b`. An instance of class `C` named `my_instance` is created with specific attributes for each class.

Python3




class A:
    def __init__(self, a):
        self.a = a
 
class B:
    def __init__(self, b):
        self.b = b
 
class C(A, B):
    def __init__(self, a, b, c):
        super().__init__(a)
        B.__init__(self, b)
        self.c = c
 
# Creating an instance of the C class
my_instance = C("A_attr", "B_attr", "C_attr")
print(f"A: {my_instance.a}, B: {my_instance.b}, C: {my_instance.c}")


Output

A: A_attr, B: B_attr, C: C_attr

Example 3: Diamond Inheritance

In this example, this Python code demonstrates multiple inheritance with classes A, B, C, and D. Classes B and C inherit from class A, and class D inherits from both B and C. The __init__ methods of classes B, C, and D utilize super().__init__() to ensure that the initialization of class A is called only once. When an instance of class D is created ( my_instance = D()).

Python3




class A:
    def __init__(self):
        print("Initializing class A")
 
class B(A):
    def __init__(self):
        super().__init__()
        print("Initializing class B")
 
class C(A):
    def __init__(self):
        super().__init__()
        print("Initializing class C")
 
class D(B, C):
    def __init__(self):
        super().__init__()
 
# Creating an instance of the D class
my_instance = D()


Output

Initializing class A
Initializing class C
Initializing class B

Conclusion

The use of super() with __init__() methods in Python provides a powerful mechanism for managing class hierarchies and ensuring that the initialization of attributes is done in a consistent and organized manner. It allows child classes to extend the behavior of their parent classes while maintaining a clear and readable code structure. Understanding and utilizing super() with __init__() methods is a key aspect of effective object-oriented programming in Python.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads