Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
super
classes

What is super() in Python?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The super() function in Python makes class inheritance more manageable and extensible. The function returns a temporary object that allows reference to a parent class by the keyword super.

The super() function has two major use cases:

  • To avoid the usage of the super (parent) class explicitly.
  • To enable multiple inheritances​.
svg viewer

Code

Single inheritance

Consider the example below, where the parent class is referred to by the super keyword:

class Computer():
    def __init__(self, computer, ram, storage):
        self.computer = computer
        self.ram = ram
        self.storage = storage

# Class Mobile inherits Computer
class Mobile(Computer):
    def __init__(self, computer, ram, storage, model):
        super().__init__(computer, ram, storage)
        self.model = model

Apple = Mobile('Apple', 2, 64, 'iPhone X')
print('The mobile is:', Apple.computer)
print('The RAM is:', Apple.ram)
print('The storage is:', Apple.storage)
print('The model is:', Apple.model)

In the example above, Computer is a super (parent) class, while Mobile is a derived (child) class. The usage of the super keyword in line 10​10 allows the child class to access the parent class’s init() property.

In other words, super() allows you to build classes that easily extend the functionality of previously built classes without implementing their functionality again.

Multiple inheritances​

The following example shows how the Super() function is used to implement multiple inheritances:

class Animal:
  def __init__(self, animalName):
    print(animalName, 'is an animal.');

# Mammal inherits Animal
class Mammal(Animal):
  def __init__(self, mammalName):
    print(mammalName, 'is a mammal.')
    super().__init__(mammalName)
    
# CannotFly inherits Mammal
class CannotFly(Mammal):
  def __init__(self, mammalThatCantFly):
    print(mammalThatCantFly, "cannot fly.")
    super().__init__(mammalThatCantFly)

# CannotSwim inherits Mammal
class CannotSwim(Mammal):
  def __init__(self, mammalThatCantSwim):
    print(mammalThatCantSwim, "cannot swim.")
    super().__init__(mammalThatCantSwim)

# Cat inherits CannotSwim and CannotFly
class Cat(CannotSwim, CannotFly):
  def __init__(self):
    print('I am a cat.');
    super().__init__('Cat')

# Driver code
cat = Cat()
print('')
bat = CannotSwim('Bat')

Consider the Cat class’s instantiation on line 3030; the following is the order of events that occur after it:

  1. The Cat class is called first.
  2. The CannotSwim parent class is called since it appears before CannotFly in the order of inheritance; this follows Python’s Method Resolution Order (MRO) which outlines the order in which methods are inherited.
  3. The CannotFly class is called.
  4. The Mammal class is called.
  5. Finally, the Animal class is called.

Next, consider the bat object. Bats are flying mammals, but they cannot swim, which is why it is instantiated with the CannotSwim class. The super function in the CannotSwim class invokes the Mammal class’s constructor after it. The Mammal class then invokes the Animal class’s constructor.

RELATED TAGS

python
super
classes
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring