super keyword in python

Posted by

What is a super keyword in python?

Every oops language support inheritance so as python. Like C++, Python also supports all forms of inheritance. In inheritance, a child class can access features of the parent class. Therefore, we can initialize features of the parent class from the child class object. The super keyword helps us to achieve this.

Super is a special function that takes instance variables of the parent class as it parameters. The super method is defined inside the init function of the child class.

Example of super keyword:

To keep this blog simple, we shall use two classes Person and Student(derived from Person class).  The person class has two instance variables those are firstname and lastname. It aslo has an instance function getfullname which returns fullname by concatenation firstname and lastname.

The Student class is derived from Person and it has an exclusive instance variable “roll”. It also has an instance function called getroll, it simply returns the roll number.

Let’s have a look at code below.

class Person:

    def __init__(self, fname, lname):

        self.firstname = fname

        self.lastname = lname

      def getfullname(self):

        return self.firstname + self.lastname

class Student(Person):

    def __init__(self, fname, lname,roll):

        self.roll = roll

        super().__init__(fname,lname)

      def getroll(self):

        return self.roll

student_obj = Student("Jhon", " Metthew",9)

print(student_obj.getfullname())

print(student_obj.getroll())

Data flow explanation:

The parameters  “Jhon” and “Matthew”  are the first and second arguments. “fname” and “lname” will hold these two strings.  Numeric 9 will be assigned with roll. All of these parameters will be present in the init method of the student class. As student class has access to the instances variable of the parent.

Inside init method Student, there will be a super method, which will take only the instance variable of the parent class. Example super().__init__(fname,lname).

The line  self.roll = roll can be there above or below super.

The picture below will show the dataflow of the parameters:

 

super python

What is the alternative route for super?

The same functionality can be achieved by explicitly defining the parent class name instead of the child class followed by init. Follow the example below:

class Person:

    def __init__(self, fname, lname):

        self.firstname = fname

        self.lastname = lname

      def getfullname(self):

        return self.firstname + self.lastname

class Student(Person):

    def __init__(self, fname, lname,roll):

        self.roll = roll

        Person.__init__(self,fname,lname)

     def getroll(self):

        return self.roll

student_obj = Student("Jhon", "Metthew",9)

print(student_obj.getfullname())

print(student_obj.getroll())

Whenever we are explicitly defining the class name,  Person.__init__(self,fname,lname)

, it must contain the self keyword in the parameter.

Explicitly defining the class name inside the derived class helps to pass parameters in case of multiple inheritances. Since each class name will be different. Hence, reduces the ambiguity.

Conclusion:

In this blog, we learned the use of super keyword, we also saw how and where we can place it, so to pass the data up in the hierarchy. In the upcoming blog, we shall learn how we can achieve multiple inheritances.

Leave a Reply

Your email address will not be published. Required fields are marked *