Python super() methodby 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
def __init__(self, val):
print("In the class TBC method")
print("The value in the class is ", val)
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 Variable
classvar = 0
def __init__(self, val):
self.val= val # Object variable
print("Object Variable has value",val)
print("Class Variable value is ", obj1.classvar)
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.
print("The name is : ",self.name)
print("The age is : ",self.age)
print("Experience : ",exper)
print("Research Area : ",rarea)
def __init__(self,name,age, course, marks):
print("course : ",course)
print("Marks : ",marks)
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:
The output of the above program is as follows:
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
class Derived(Base1,Base2): # Derived Class derived from Base1 and Base2
The output of the above code will be
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.
Created on Mar 16th 2020 05:04. Viewed 239 times.