Python super() – A Simple Illustrated Guide

Python’s built-in super() method returns a temporary object of the superclass to help you access its methods. Its purpose is to avoid using the base class name explicitly. It also enables your class to inherit from multiple base classes.

Visual Idea super()

The idea is simple: use super() to call the methods defined in the parent classes—whether your child class inherits from one or multiple base classes. See the graphic:

Python super() - Visual Explanation

Need me to guide you through this example? Watch the explainer video next!

Video super() by Example

Next, you’ll learn about both cases by example!

Simple Example 1: super() with Single Inheritance

Inheritance in object-oriented programming allows you to create a class hierarchy where one child class inherits all methods from another parent class. This simplifies the development of large software projects and avoids redundant code. You can learn more about the concept of inheritance in our blog tutorial here.

For example, the following code defines the parent class Organism and the child class Human. The child class uses super() to run the constructor method of the parent class.

class Organism:
    def __init__(self):
        print('I live')


class Human(Organism):
    def __init__(self):
        print('I am human')
        super().__init__()


alice = Human()

The output is:

I am human
I live

Here you call the base class Organism using the following code:

super().__init__()

A semantically equivalent code call would be:

Organism.__init__(self)

You call the __init__() method on the base class Organism and pass a reference to the calling instance as an argument. This way, you could also modify the internal attributes of the self instance within the base class’ constructor.

However, the advantage of using super().__init__() compared to ParentClass.__init__(self) is that you avoid calling the parent class explicitly. This is advantageous because it decouples the child from the parent class. For example, if you changed the name of the ParentClass to NewParentClass, the method using super() would be superior because it would still work while the method using ParentClass.__init__(self) would throw an error.

Example 2: super() with Multiple Inheritance

One of Python’s unique features compared to other programming languages is that it allows multiple inheritance.

Multiple inheritance means that a class can inherit from multiple parents. For example, a class Human can inherit from two parent classes: Organism and Thinker. Say, you define a method live() in Organism and think() in Thinker. If a Human object inherits from both classes, it can call live() and think() at the same time! You use the super() method to call those functions:

class Organism:
    def live(self):
        print('I live')


class Thinker:
    def think(self):
        print('I think')


class Human(Organism, Thinker):
    def __init__(self):
        print('I am human')
        super().live()
        super().think()
 

alice = Human()

The output is:

I am human
I live
I think

I should mention that in this example, you could also have called self.live() and self.think() in the class Human instead of super().live() and super().think(). The output would be the same in both cases. In practice, you’d use the former for instance methods and the latter for class methods. The difference between both is explained in our blog tutorial here.

Summary

Python’s built-in super() method returns a temporary object of the superclass to help you access its methods. Its purpose is to avoid using the base class name explicitly. It also enables your class to inherit from multiple base classes.

You can dive deeper into some of the quirks of the super() method here:

If you’ve spent an hour studying all three resources, you’ve become a super() superstar—and it’ll help you throughout your programming career. A good return on invested time!

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!