×

Python Super function

The super function is used to access and call the methods or functions involved in the parent class during Inheritance.

What is Inheritance?

The process where the parent class inherits some of its members, including methods, variables, etc., to the child class or subclass is known as Inheritance.

Condition for implementing super function:

The super function should contain the same arguments as the function that is called by the object.

Uses of super function:

  • We need not specify the methods and members present within the parent class while calling them from the child class / sub-class.
  • Supports code optimization and reusability as there is no need to rewrite the functions that are being called.
  • Working with Multiple Inheritance can proceed with the help of the super() function. So, it obeys Multiple Inheritance.

The super function can be implemented in:

  1. Single Inheritance
  2. Multiple Inheritance

Syntax / General usage of Super function:

super().__init__()

Single Inheritance using Super function:

If the child classes inherit members only from a single parent class, then that inheritance is called Single Inheritance. It consists of a Child class that inherits members from a Parent class. 

Super function in Python

We shall discuss the implementation of the super function in Single Inheritance with an example.

Example:

Let’s consider an example of vehicles. Cycles, Bikes, Cars, etc., come under the category of vehicles. They share a common point, i.e., we can ride all of them, and they make transportation easier.

So, we can conclude that these classes are the child classes of Vehicles (a parent class for all those classes). For example, let's frame a program using a super function and find the output.

Parent class: Vehicles

Child classes: Cycles, Bikes, Cars.

Multiple Inheritance using Super function:

If the child classes inherit members from more than one parent class, then that inheritance is called Multiple Inheritance. It consists of Child class which inherits members from two parents i.e., Father class and Mother class.

Super function in Python

We shall discuss the implementation of the super function in Multiple Inheritance with an example.

Example:

Let’s consider an example of Animals. Mammals, Non-winged Mammals, Non-marine Mammals, etc., also come under the category of Animas. They share a common point, i.e., they are Animals. Non-winged Mammals, Non-marine Mammals also have a common point, i.e., they belong to the mammals' category.

So, we can conclude that these classes are the child classes of Animals (which is a parent class for all those classes) and Non-winged Mammals, and Non-marine Mammals classes are the child classes of Mammals class. Again, being a mammal, “Non-winged Mammals” and “Non-marine animals” are a part of “Animals”. Now, let a class Dog be taken. It is a combination of Non-winged mammals and Non-marine mammals. So, the dog undergoes Multiple Inheritance.

Parent class: Animals.

Child class: Mammals.

Grandchild classes: Non-winged Mammals, Non-marine Mammals

Great grandchild class: Dog(Non-winged Mammals, Non-marine Mammals)

Here, the father class is the " Non-winged Mammals “ class, whereas the mother class is the " Non-marine Mammals “ class.

Let's consider an example program which describes a super function.

Program:

class vehicles:
    def __init__(self,w,p):
        self.wheels = w
        self.passenger = p
    def wheelNo(self):
        print("The number of wheels are ")
        print(self.wheels)
    def passengerNo(self):
        print("The maximum number of passengers are ")
        print(self.passenger)


class Bikes(Vehicles):
    def __init__(self,w,p):
        print("Bikes: ")
        super().__init__(w,p)






class Cycles(Vehicles):
    def __init__(self,w,p):
        print("Cycles: ")
        super().__init__(w,p)


class Cars(Vehicles):
    def __init__(self,w,p):
        print("Cars: ")
        super().__init__(w,p)




# objects being created in child classes
cy1 = Cycles(2,2)
cy1.wheelNo()
cy1.passengerNo()
b1 = Bikes(2,2)
b1.wheelNo()
b1.passengerNo()
ca1 = Cars(4,5)
ca1.wheelNo()
ca1.passengerNo()

Output:

Cycles: 
The number of wheels are 
2
The maximum number of passengers are 
2
Bikes: 
The number of wheels are 
2
The maximum number of passengers are 
2
Cars: 
The number of wheels are 
4
The maximum number of passengers are 
5

Explanation:

The classes " Bikes ", " Cycles " and " Cars " are inherited from the class " Vehicles ". So, " Vehicles " is the parent class whereas the classes Bike, Cycles and Cars are the child classes of Vehicle. The methods that define the number of wheels and passengers are declared in the parent class " Vehicles ". But, we have created objects in the child class. Although, we can access those methods when we use the super function. As the super function is used in all three child classes, the statements present in the parent class methods have been printed.


Related Topics

List Comprehension in Python

Sometimes we need new lists that are completely based on previously existing lists, so to perform this operation successfully, some of the programming languages come with a syntactic construct known...

5 minutes read.

Sublime Python

SUBLIME: A compact, cross-platform code editor called Sublime Text 3 (ST3) is well-known for its quickness, usability, and robust community support. Although it's a fantastic editor out of the box, its...

6 minutes read.

Python Modulo

For basic calculations, Python provides operators. Python supports a broad range of Arithmetic Operators to do arithmetic, as given below: +Addition*Multiplication-Subtraction/Division//Floor division**Exponentiation%Remainder/Modulus As you can see, one of these basic arithmetic operators...

8 minutes read.

Python Compiler

What is Compiler? The compiler is mainly a program used to convert the source code into the machine or binary code. The source code is generally a computer program written using...

6 minutes read.

How to plot multiple linear regression in Python

This article lets us look into linear regression in Python and how we can plot multiple linear regressions in it. Linear regression One of the simplest and most widely used Machine Learning...

4 minutes read.

Python Dictionary keys() method

Python Dictionary keys() method The dictionary.keys() method in Python returns a view object that displays a list of all the keys in the dictionary Syntax dictionary.keys() Parameter NA Return This method returns a view object that displays...

2 minutes read.

Python setattr() function

Python setattr() function The setattr() function sets the value of the specified attribute of the specified object. Syntax setattr(object, name, value) Parameter object: This parameter represents any object. name: This parameter represents the name of the attribute you...

1 minute read.

BOTTLE Python Web Framework

The Bottle is a lightweight WSGI micro web framework for python. It acts like a thin wrapper around a web server where it is distributed as a single file module...

4 minutes read.

How to develop a game in python

Fun always makes a task interesting and easy to execute. Learning in the same theoretical way at some point reaches the boring spot. In this article, using some Python knowledge,...

13 minutes read.

Python for loop

 Python for loop A for loop in Python executes a block of code for a specified number of times, based on a given sequence. The for loop in Python is different than any other...

3 minutes read.

Scrimba python

Scrimba allows you to study whenever and wherever the topics or concepts you want. It also replaces classroom instruction with interactive screencasts, live events, and student-to-student help. Scrimba is an interactive...

3 minutes read.

Python String isalpha() method

Python String isalpha() method The string.isalpha () method in Python returns a boolean value true if all characters in the  string are alphabetic else for any other value it returns false. Syntax isalpha()  Parameter NA Return This...

1 minute read.

How to Import Files in Python

Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Difference between Sort and Sorted in Python

If you new to Python, it must be confusing the distinction between the Sort and Sorted functions. However, it is important to understand the differences in order to use them...

5 minutes read.

Python Parallel Processing

By performing more jobs concurrently, your software may complete more tasks in a shorter amount of time. These aid in solving major issues. The following subjects will be covered in...

2 minutes read.

Python Ways to find nth occurrence of substring in a string

Introduction In Python, a string is a collection of characters that can be employed to conduct additional operations. In Python, a substring is a group of characters that are a part...

4 minutes read.

CSV Write in Python

What is meant by CSV? CSV stands for Comma Separated Values. The name itself defines its purpose. CSV arranges the data in the form of tables and stores the organized data in...

3 minutes read.

Nested for Loop in Python

"Loop" is one of the foundation concepts to learn programming in any language. Nested loops are significant steps in solving many types of problems, ranging from basic to complex scenarios....

9 minutes read.

Python OOPs: Class, Object, Inheritance and Constructor

Basic Object-Oriented Programming (OOP) Concepts in Python Python is similar to most general-purpose programming languages with an Object-Oriented environment system since its existence. Being an Object-Oriented Programming language, Python provides ease...

9 minutes read.

Returning Multiple Values in Python

Python is considered a general-purpose programming language; it is a high-level programming language that is not much difficult and easier to learn. It is rich in libraries that can be...

3 minutes read.