Last Updated:

Super() method in Python

 

The built-in super() returns a proxy object (a temporary superclass object), allowing us to access the base class's methods.

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

The super() function has two main uses:

  • To avoid explicit use of the super (parent) class.
  • To enable multiple inheritance.

Single inheritance

 

Consider an example below where the parent class is called the super keyword:

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

# Class Mobile inherits Computer
classMobile(Computer):
def __init__(self, computer, RAM, storage capacity,model):
super().__init__(computer, RAM, storage capacity)
self.model = model

Apple = Mobile('Apple', 2, 64, 'iPhoneX')
print('Mobile Phone:', Apple.computer)
print('RAM:', Apple.ram)
print('Storage Capacity:', Apple.storage)
print('Model:', Apple.model)

Output:

Mobile Phone: Apple
RAM: 2
Storage Capacity: 64
Model: iPhoneX

In the example above, Computer is the super (parent) class and the mobile-derived (child) class. Using the super keyword on line 10 allows a child class to access the init() property of the parent class.

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

Multiple inheritance

 

The following example shows how the Super() function is used to implement multiple inheritance:In the example above, Computer is the super (parent) class and the Mobile-derived (child) class. Using the super keyword on line 10 allows a child class to access the init() property of the parent class.

In other words, super() allows you to create classes that easily extend the functionality of previously built classes without implementing their functionality again. The following example shows how the Super() function is used to implement multiple inheritance:

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

# A mammal inherits
class Mammal (Animal):
def __init__(self, mammalName):
print(mammalName, 'it is a mammal'.)
super().__init__(mammalName)

# CannotFly inherits
class Mammal CannotFly:
def __init__(self, mammalThatCantFly):
print(mammalThatCantFly, "cannot fly.")
super().__init__(mammalThatCantFly)

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

# Cat inherits CannotSwim andCannotFly
classCat(CannotSwim, CannotFly):
def __init__(self):
print("I am a cat".);
super().__init__(‘Cat’)

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

Output:

I'm a cat.
The cat can't swim.
The cat can't fly.
The cat is not a mammal.
The cat is an animal.

A bat cannot swim.
The bat is a mammal.
Bat animal.

Consider an instance of the Cat class; the following is the order of events that occur after it:

  • The Cat class is called the first.
  • The parent CannotSwim class is called because it appears before CannotFly in the order of inheritance; this follows the Python Method Resolution Order (MRO), which describes the order in which the methods are inherited.
  • The CannotFly class is called.
  • The class of mammals is called.
  • Finally, the Class of Animals is called.
  • Next, consider the bat object. Bats are flying mammals, but they can't swim, which is why it's created with the class CannotSwim. The superfunction in the CannotSwim class calls the constructor of the Mammal class after it. The Mammal class then calls the constructor of the Animal class.

Polymorphism in Python

 

The literal meaning of polymorphism is a condition for the emergence of various forms. Polymorphism is a very important concept in programming. This refers to using an entity of the same type (method, operator, or object) to represent different types in different scenarios. Let's take an example.

Polymorphism in the addition operator

We know that the + operator is widely used in Python programs. But it doesn't have a single use.
For integer data types, the + operator is used to perform an arithmetic addition operation.

num1 = 1
num2 = 2
print(num1+num2)

Therefore, the above program outputs 3. Similarly, for string data types, the + operator is used to perform concatenation.

str1 = "Python"
str2 = "Programming"
print(str1+" "+str2)

As a result, the above program outputs:

Python Programming

Here we can see that a single + operator was used to perform different operations on different types of data. This is one of the simplest manifestations of polymorphism in Python.

Function Polymorphism in Python

 

Python has some functions that are compatible for working with multiple types of data.
One such function is the len() function. It can work with many types of data in Python. Let's look at a few examples of using this feature.

Polymorphic function len()

print(len(«Programming»))
print(len([«Python», «Java», «C»]))
print(len({«Name»: «John», «Address»: «USA»}))

Exit

11
3
2

Here we can see that many data types, such as string, list, tuple, set, and dictionary, can work with the len() function. However, we can see that it returns specific information about specific types of data.
Note: Method overloading, a way to create multiple methods with the same name but different arguments, is not possible in Python.