Using the super() Function in Python

python super

In Python, when a class is defined, it inherits attributes and methods from its parent class. The parent class is also known as the superclass, and the class that inherits from it is called the subclass. The super() function is used to call a method from the superclass in the subclass.

How to Use Python Super

Example 1: Calling a Superclass Method

Let’s consider an example where we have a class Person that has a method greet(). We want to create a subclass Student that inherits from Person but overrides the greet() method. We can use super() to call the greet() method of the superclass.

class Person:
    def greet(self):
        print("Hello, I am a person")

class Student(Person):
    def greet(self):
        super().greet()
        print("I am a student")

s = Student()
s.greet()

Output:

Hello, I am a person
I am a student

Example 2: Calling a Superclass Constructor

We can also use super() to call the constructor of the superclass. In the following example, we have a class Animal that has a constructor that initializes the name attribute. We want to create a subclass Dog that inherits from Animal but also initializes the breed attribute. We can use super() to call the constructor of the superclass.

class Animal:
    def __init__(self, name):
        self.name = name

class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name)
        self.breed = breed

d = Dog("Max", "Labrador")
print(d.name)
print(d.breed)

Output:

Max
Labrador

Example 3: Multiple Inheritance

super() is also useful in multiple inheritance scenarios. When a class inherits from multiple classes, the super() function allows us to call methods of all the parent classes in a consistent order.

class A:
    def greet(self):
        print("Hello from A")

class B:
    def greet(self):
        print("Hello from B")

class C(A, B):
    def greet(self):
        super().greet()

c = C()
c.greet()

Output:

Hello from A

Example 4: Using Super Outside of a Class

super() can also be used outside of a class, although this is not a common use case. In the following example, we create a function greet() that takes an object as an argument and calls the greet() method of its superclass.

class Person:
    def greet(self):
        print("Hello, I am a person")

class Student(Person):
    def greet(self):
        print("I am a student")

def greet(obj):
    super(type(obj), obj).greet()

s = Student()
greet(s)

Output:

Hello, I am a person

Example 5: Using Super with a Mixin

A mixin is a class that provides additional functionality to another class without being the parent class of that class. Mixins are often used to add common functionality to multiple classes. In the following example, we have a mixin Logger that provides a log() method. We want to create a subclass Student that inherits from Person and also uses the Logger mixin. We can use super() to call the log() method of the mixin.

class Logger:
    def log(self, message):
        print(message)

class Person:
    def greet(self):
        print("Hello, I am a person")

class Student(Person, Logger):
    def greet(self):
        super().greet()
        super().log("I am a student")

s = Student()
s.greet()

Output:

Hello, I am a person
I am a student

Conclusion

In this article, we discussed the super() function in Python and provided code examples to illustrate its usage. We saw how super() can be used to call a superclass method, call a superclass constructor, handle multiple inheritance, use super() outside of a class, and use super() with a mixin. super() is a powerful tool in the Python arsenal and is essential for working with inheritance in object-oriented programming.