Introduction

Super() is a built-in function that allows you to call methods of a parent class from a subclass. By using super(), you can easily access and override methods that are already defined in a parent class, allowing you to add functionality without duplicating code. This tutorial will cover everything you need to know about using the super() function in Python, including how it works, when to use it, and some practical examples to help reinforce your understanding. Whether you're new to Python or a seasoned developer looking to level up your skills, understanding super() is an essential part of mastering object-oriented programming in Python.

Table of Contents :

  • super() Function
  • Syntax
  • Usage

super() Function :

  • `super()` is a built-in function in Python that returns a temporary object of the superclass, which allows us to call methods of the superclass.
  • It is used to implement inheritance and enables us to reuse the code of the superclass in the subclass.

Syntax:

  • The `super()` function has two syntaxes:
    •  super().(): to call a method in the superclass
    •  super(, ).(): to call a method in a specific superclass from a subclass

Usage:

  • The `super()` function is used for the following purposes:
    • To call a method of the superclass that has been overridden in the subclass
    • To call a method of the superclass from the subclass
    • To avoid hard-coding the superclass name in the subclass

Example #1: 

Calling a Method of the Superclass That Has Been Overridden in the Subclass


class Animal:
   def speak(self):
       print("This animal speaks")

class Dog(Animal):
   def speak(self):
       print("This dog barks")
d = Dog()
d.speak()           # Output: This dog barks

class Bulldog(Dog):
   def speak(self):
       print("This Bulldog growls")
b = Bulldog()
b.speak()           # Output: This Bulldog growls

class Pug(Dog):
   def speak(self):
       super().speak()
       print("This Pug snorts")

p = Pug()
p.speak()           

# Output: This dog barks \n This Pug snorts


 

Example #2: 

Calling a Method of the Superclass from the Subclass


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

class Dog(Animal):
   def __init__(self, name, breed):
       self.name = name
       self.breed = breed
       super().__init__("Dog")
   def speak(self):
       print("This dog barks")

class Pug(Dog):
   def __init__(self, name):
       super().__init__(name, "Pug")
   def speak(self):
       super().speak()
       print("This Pug snorts")

p = Pug("Puggy")
print(p.species)    

# Output: Dog


Example #3: 

Avoiding Hard-Coding the Superclass Name in the Subclass


class Shape:
   def __init__(self, x, y):
       self.x = x
       self.y = y

class Circle(Shape):
   def __init__(self, x, y, r):
       self.r = r
       super().__init__(x, y)

c = Circle(0, 0, 5)
print(c.x, c.y, c.r)       

# Output: 0 0 5


 

Prev. Tutorial : Hybrid inheritance in Python

Next Tutorial : Method Overriding