Python super() keyword

Python super() keyword

Introduction

In Python, the super() keyword is a built-in method that returns a proxy object (object of a superclass that is temporary). It allows us to access methods present in the base class.

Uses of super()

In Python, the super() keyword has two major uses:

  • It allows us to avoid using the name of the base class explicitly.
  • Works with multiple inheritances.

Example 1

Code

class Animal(obj):
  def __init__(self, a_type):
    print('Animal Type:', a_type)
   
class Mammal(Aml):
  def __init__(self):

    super().__init__('Mammal')
    print('Mammals give birth directly')
   
dog = Mammal()

Output

Animal Type: Mammal
Mammals give birth directly

write your code here: Coding Playground

Example 2: super() with Single Inheritance

Code

class Mam(object):
  def __init__(self, m_Name):
    print(m_Name, 'is a warm-blooded animal.')
   
class Dog(Mam):
  def __init__(self):
    print('Dog has four legs.')
    super().__init__('Dog')
   
d1 = Dog()
   
class Dog(Mammal):
  def __init__(self):

Output

Dog has four legs.
Dog is a warm-blooded animal.

write your code here: Coding Playground

Example 3:super() with Multiple Inheritance

Code

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

class ml(al):
  def __init__(self, m_Name):
    print(m_Name, 'is a warm-blooded animal.')
    super().__init__(m_Name)
   
class NonWingedml(ml):
  def __init__(self, NonWingedml):
    print(NonWingedml, "can't fly.")
    super().__init__(NonWingedml)

class NonMarineml(ml):
  def __init__(self, NonMarineml):
    print(NonMarineml, "can't swim.")
    super().__init__(NonMarineml)

class Dog(NonMarineml, NonWingedml):
  def __init__(self):
    print('Dog has 4 legs.');
    super().__init__('Dog')
   
d = Dog()
print('')
bat = NonMarineml('Bat')

Output

Dog has 4 legs.
Dog can't swim.
Dog can't fly.
Dog is a warm-blooded animal.
Dog is an animal.

Bat can't swim.
Bat is a warm-blooded animal.
Bat is an animal.

write your code here: Coding Playground

In the above code, we called the init(0 function of the ml class (from the Dog class) using code

super().__init__('Dog')

instead of

Mammal.__init__(self, 'Dog')

We can change the name of the base class easily because we do not have to mention the name of the base class when we call its members.

# changing base class to CF
class Dog(CF):
  def __init__(self):
    print('Dog has four legs.')

    # changing this oios not requiremnt
    super().__init__('Dog')

The super() is a built python method that returns a proxy object, and it is a substitute object that can invoke the base class’s members using delegation. This is indirection, i.e. ability to reference the base class with super()).

Since it is computed at runtime, we can use different base classes at different times.

Method Resolution Order (MRO)

MRO (Method Resolution Order) is an order. In this order, the method should b in the existence of multiple inheritances. MRO can also be viewed by using the mro attribute.

>>> Dog.__mro__
(<class 'Dog'>,
<class 'NonMarineMammal'>,
<class 'NonWingedMammal'>,
<class 'Mammal'>,
<class 'Animal'>,
<class 'object'>)

Working of MRO

  • Before the calling of the base class, the method in the derived call is called always.
  • In the example above, we called the Dog class before NonMArineml, which is also invoked before al, and the al class is also invoked before the object.
  • If there exist multiple parents such as Dog(Nonmarineml, NonWingedml) because NonMarineml appears first that is why it is also invoked first.