Articles

Python super() method

by nishith srivastava Software Developer

Python super() method


As we all know that Python Language supports the concept of an object-oriented language it also supports the concept of classes and objects. As classes and objects are the two very important topics in object-oriented programming, let us first revise a little bit about classes and objects. A class is the building block of Python language. A class creates a new type and an object is an instance (variable) of the specific class. The super() method is used when the inheritance is used in Python Language. For understanding it completely we must know the concept of polymorphism and method overriding. Polymorphism, in simple terms, refers to attaining different forms when required. It is one of the very important features of Object-Oriented Programming. It gives the programmers to assign a different meaning or usage to a variable, function or object in a different context. We could note that inheritance is related to classes and their hierarchy, polymorphism is mostly related to the methods. Another concept which we must understand while understanding the super() method is the __init__() method. The _init_() method has a very special significance in Python classes. The _init_() method is automatically executed when an object of a class is created. This method behaves like the automatic constructors in other object-oriented programming languages like C++, JAVA, VC++, C#, etc. The __init__() method is useful to initialize the variables of the class objects. let's understand the usage of the _init_() method with the help of a small program

class TBC(int):

    def __init__(self, val):

        print("In the class TBC method")

        self.val=val

        print("The value in the class is ", val)

obj1=TBC(100)

The output of the above program is:

In the class TBC method

The value in the class is 100

Here the important thing which we have to take care of is that __init__() is prefixed and suffixed by double underscores. The __init__() method can be declared as, def __init__(self,[args.....]).

Now let us understand another term class variable and object variables with the help of a program

class TBC(int):

    # Class Variable

    classvar = 0 

    def __init__(self, val):

        TBC.classvar1=TBC.classvar+1

        self.val= val # Object variable

        print("Object Variable has value",val)

        print("Class Variable value is ", obj1.classvar)

obj1=TBC(100)

obj2=TBC(200)

obj3=TBC(500)

Now the meaning which we can deduce from the above code is as follows:

  • If a class has n objects, then there will n different copies of the object a variable which means with each instance new object is created automatically.
  • The object variable is not shareable to any other object.
  • A change made to the object variable by one object will remain in the same object and does not get reflected in any other object.
  • The number of variable in the class decide how many copies will be created for that class. All the objects of that class will share the class variable.
  • Any change made in the class variable will be reflected in different objects as only a single copy of the class, a variable is created.

Now, let us move back to our concept of inheritance. 

class person:

    def __init__(self,name,age):

        self.name=name

        self.age=age

    def show(self):

        print("The name is : ",self.name)

        print("The age is : ",self.age)

class teacher(person):

    def __init__(self,name,age,exper,rarea):

        person.__init__(self,name,age)

        self.exper=exper

        self.rarea=rarea

    def displaydata(self):

        person.show(self)

        print("Experience : ",exper)

        print("Research Area : ",rarea)

class student1(person):

    def __init__(self,name,age, course, marks):

        person.__init__(self,name,age)

        self.course=course

        self.marks=marks

    def displaydata(self):

        person.show(self)

        print("course : ",course)

        print("Marks : ",marks)

print("Teacher")

t=teacher("Ashish",40,"20","Computer Security")

t.displaydata()

print("Student")

s=student1("Sparsh",15,"MCA",90)

s.displaydata()

The above code shows hoe inheritance work. Now here the base class is person whoes information is inherited by two other class teacher and student1. from teacher its super class is person. Infact the person is the super class for both teacher and student1 derived class.

Now we understand the use of the super() method with the help of another program:

class Base1(object):

    def __init__(self):

        print("Base1 class")

        super(Base1,self).__init__()

class Base2(object):

    def __init__(self):

        print("Base2 Class")

class Derived(Base1,Base2):

    pass

D=Derived()

The output of the above program is as follows:

Base1 class

Base2 Class

To completely understand the concept of the super() the method we must recall method overriding. The method overriding is the ability of a class to change the implementation of a method provided earlier or by its ancestral class. For using the function defined in the parent class we use the super() method. The super() method is the built-in method or function that denotes the base class. This means that when we invoke the super() method it means that we are referring to the parent version of the method that had been called.

We can also use the super() method in Multiple Inheritance where the derived class share the properties or inherit the properties of more than one class. Let us understand this concept by the help of a program:

class Base1(object):# First Base Class

    def __init__(self):

       super(Base1,self).__init__()

       print("Base1 class")

class Base2(object):

    def __init__(self):

        super(Base2,self).__init__()

        print("Base2 Class")

class Derived(Base1,Base2): # Derived Class derived from Base1 and Base2

    def __init__(self):

        super(Derived,self).__init__()

        print("Derived Class")

D=Derived()

The output of the above code will be

Base2 Class

Base1 class

Derived Class

So to conclude we must remember that the super() a method is the in-built function which is used to refer the base class when used in the derived class.

 

Tutors Group

 

 


Sponsor Ads


About nishith srivastava Freshman   Software Developer

6 connections, 0 recommendations, 29 honor points.
Joined APSense since, February 25th, 2019, From lucknow, India.

Created on Mar 16th 2020 05:04. Viewed 382 times.

Comments

No comment, be the first to comment.
Please sign in before you comment.