What Does Super Do In Python

2 min read

In Python object-oriented programming, one of the most powerful yet misunderstood tools is the super() function. If you have ever wondered what does super do in Python, you are not alone. Many developers encounter super() when working with inheritance, constructors, and method overriding, but few fully grasp how it manages method resolution across parent and child classes. At its core, super() allows a child class to call methods from its parent class without explicitly naming the parent, making your code more flexible, maintainable, and ready for complex inheritance hierarchies Less friction, more output..

What Is super() in Python?

The super() function returns a temporary object that allows you to refer to the parent class by proxy. Instead of calling a parent method by hardcoding the parent class name, you use super() to let Python resolve which method should be executed next in the inheritance chain Simple, but easy to overlook..

This is especially useful in object-oriented programming because it decouples your child class from the specific name of its parent. If you later rename the parent class or reorganize your class hierarchy, code that relies on super() continues to work without modification Worth knowing..

super() is most commonly used inside the init method to check that the parent class constructor runs properly before the child class adds its own initialization logic Less friction, more output..

How super() Works in Single Inheritance

In single inheritance, a child class inherits from exactly one parent class. Here is a basic example:

class Parent:
    def __init__(self):
        print("Parent class initialized")

class Child(Parent):
    def __init__(self):
        super().__init__()
        print("Child class initialized")

obj = Child()

Output:

Parent class initialized
Child class initialized

In this example, super().Now, init() tells Python to look up the parent class and execute its init method. Without that line, the parent constructor would be skipped entirely, and any setup logic defined there would never run That's the part that actually makes a difference..

You can also use super() with regular methods, not just constructors:

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

class Child(Parent):
    def greet(self):
        super().greet()
        print("Hello from Child")

When you call greet() on a Child instance, Python first runs the Parent version, then the Child version. This pattern lets you extend behavior instead of completely replacing it Not complicated — just consistent. But it adds up..

super() and Method Resolution Order (MRO)

One reason super() is so powerful is that it does not simply jump to the immediate parent. Still, it follows the Method Resolution Order, or MRO. The MRO is the linearized order in which Python searches for methods and attributes in a class hierarchy.

Python uses an algorithm called C3 linearization to calculate the MRO. You can inspect it at any time using the mro attribute:

class A:
    pass

class B(A):
    pass

print(B.__mro__)

Output:

(
Up Next

Trending Now

More of What You Like

More to Chew On

Thank you for reading about What Does Super Do In Python. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home