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.
Example
#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)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
#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
#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())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"))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()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__, callsuper().__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
#- InheritanceBuilding one class on top of another: the derived class inherits the base class’s methods and can extend or override them.
- __init__()A method called automatically when an object is created. It stores the object’s initial data, its attributes.
- classDefines a class: a template for objects that combines data with the functions that act on it.
- *args and **kwargsCollecting any number of positional arguments into a tuple and keyword arguments into a dictionary, plus unpacking a collection at call time.
Spotted a mistake or missing an example? Write to us.