super()

Calls a method of the base class from inside the derived class, most often the __init__ constructor.

Returns
A proxy object through which you call methods of the next class in MRO order, usually the base class.
On this page

Example

#
Python
class User:
    def __init__(self, name):
        self.name = name
        self.active = True

class Student(User):
    def __init__(self, name, world):
        super().__init__(name)
        self.world = world
        self.points = 0

mia = Student("Mia", 7)
print(mia.name, mia.active)
print(mia.world, mia.points)
Resultsaved result, you can check it
Mia True
7 0

Definition and usage

#

The super() function returns a proxy object through which you call a method of the base class. It is most often used in the derived class’s __init__: super().__init__(name) lets the base class set up its own attributes, while the derived class adds only the new ones.

You extend any overridden method the same way. Instead of copying code from the base class, you call super().method() and add something before or after it. If you skip this, the base version never runs at all. For __init__, that means the attributes the base class sets up go missing.

Inside a method, writing super() with no arguments is enough, because Python figures out the class and the object itself. Under multiple inheritance, super() does not simply point to "the parent": it points to the next class in the object’s MRO order. That way, every class in the hierarchy runs exactly once, as long as all of them consistently call super().

Syntax

#
Syntax
class Derived(Base):
    def __init__(self, a, b):
        super().__init__(a)
        self.b = b

    def method(self):
        result = super().method()
        ...

Parameters

#
  • type

    class, optional

    The class after which the method search in MRO order begins. Omitted inside methods.
  • object_or_type

    object or class, optional

    The object you are calling the method for. Omitted inside methods.

More examples

#
Extending an overridden method
Python
class Reward:
    def __init__(self, exp):
        self.exp = exp

    def describe(self):
        return f"+{self.exp} EXP"

class StreakReward(Reward):
    def __init__(self, exp, days):
        super().__init__(exp)
        self.days = days

    def describe(self):
        return super().describe() + f" and a {self.days}-day streak"

print(Reward(10).describe())
print(StreakReward(15, 7).describe())
Resultsaved result, you can check it
+10 EXP
+15 EXP and a 7-day streak
Without super().__init__(), attributes are missing
Python
class User:
    def __init__(self, name):
        self.name = name

class Mentor(User):
    def __init__(self, name, course):
        self.course = course

darwin = Mentor("Darwin", "Python")
print(darwin.course)
print(hasattr(darwin, "name"))
Resultsaved result, you can check it
Python
False
super() under multiple inheritance
Python
class Lesson:
    def start(self):
        print("Lesson: loading content")

class WithTimer(Lesson):
    def start(self):
        print("WithTimer: starting the timer")
        super().start()

class WithHints(Lesson):
    def start(self):
        print("WithHints: preparing hints")
        super().start()

class Quiz(WithTimer, WithHints):
    def start(self):
        print("Quiz: start")
        super().start()

Quiz().start()
Resultsaved result, you can check it
Quiz: start
WithTimer: starting the timer
WithHints: preparing hints
Lesson: loading content

super() in the WithTimer class calls the method from WithHints, not from Lesson, because that is the MRO order of the Quiz object. This way, Lesson.start() runs only once.

Good practices

#
  • In a derived class’s __init__, call super().__init__(...) at the start, before you use any attributes the base class sets up.
  • Do not hardcode the base class’s name, e.g. User.__init__(self, name). super() keeps working correctly even after the hierarchy changes, and under multiple inheritance too.
  • Under multiple inheritance, every class in the chain should call super(). A single class that skips this call breaks the whole chain.

Related entries

#

Spotted a mistake or missing an example? Write to us.