Python super() Function – Tutorial with Examples

The super() function in Python is a built-in function that is used to refer to the parent class and its methods. It is commonly used in inheritance, where a subclass wants to call methods from its parent class. The super() function allows you to call methods from a parent class, or a class that the current class is derived from.

Syntax

super([type[, object-or-type]])

Parameters

  • type : The type of the parent class. This parameter is optional, and if not specified, Python will try to infer the type from the current class.
  • object-or-type : The object or type to be used as the instance or type, respectively, for resolving the method call. If not specified, the first argument should be a type, and the function will return a temporary object of the specified type, which allows you to call its methods. If the first argument is an object, the second argument should be a type and the function will return a delegate object that allows you to call methods on the object, as if it were an instance of the specified type.

Return Value

The super() function returns a temporary object of the specified type, which allows you to call its methods. If the type argument is not specified, the function will try to infer the type from the current class, and if that is not possible, a TypeError will be raised.

Examples

Example 1: Using super() in simple inheritance

# Simple inheritance example
class Parent:
    def __init__(self, value):
        self.value = value
    def show(self):
        print("Value in Parent:", self.value)
        
class Child(Parent):
    def __init__(self, value):
        super().__init__(value)
    def show(self):
        super().show()
        print("Value in Child:", self.value)
        
c = Child(10)
c.show()

Output

Value in Parent: 10
Value in Child: 10

Example 2: Using super() with multiple inheritance

# Multiple inheritance example
class GrandParent:
    def __init__(self, value1):
        self.value1 = value1
    def show(self):
        print("Value1 in GrandParent:", self.value1)
        
class Parent(GrandParent):
    def __init__(self, value1, value2):
        super().__init__(value1)
        self.value2 = value2
    def show(self):
        super().show()
        print("Value2 in Parent:", self.value2)
        
class Child(Parent):
    def __init__(self, value1, value2, value3):
        super().__init__(value1, value2)
        self.value3 = value3
    def show(self):
        super().show()
        print("Value3 in Child:", self.value3)

c = Child(10, 20, 30)
c.show()

Output

Value1 in GrandParent: 10
Value2 in Parent: 20
Value3 in Child: 30

Example 3: Using super() with multiple inheritance and method resolution order (MRO)

# Multiple inheritance with MRO example
class A:
    def show(self):
        print("In class A")
        
class B(A):
    def show(self):
        print("In class B")
        super().show()
        
class C(A):
    def show(self):
        print("In class C")
        super().show()
        
class D(B, C):
    def show(self):
        print("In class D")
        super().show()
        
d = D()
d.show()

Output

In class D
In class B
In class C
In class A

Use Cases

The super() function is commonly used in inheritance when a subclass needs to call methods from its parent class. It can also be used in multiple inheritance when a class needs to call methods from multiple parent classes, and the method resolution order (MRO) is used to determine which method to call. The super() function is also useful when a class wants to extend the functionality of its parent class without completely replacing it.

In conclusion, the super() function is an essential tool for object-oriented programming in Python, and understanding its usage can greatly improve the readability and maintainability of your code.

Leave a Reply

Your email address will not be published. Required fields are marked *