+

Python super () method in detail

Today in this tutorial we will discuss the super () method in Python. Before diving into the topic, we strongly recommend that you study the Python inheritance tutorial .

super

super method returns a proxy object that delegates method calls to the parent or sibling class of the type. This is useful for accessing inherited methods that have been overridden in the class.



Or just use it to call the constructor, that is, the __init __ () superclass method.

Syntax.

 super ([type [, object-or-type]]) 

B in In Python 3.x versions, we can use super without passing the above two parameters. Take a look at the below code snippet.

 class C (B): def method (self, arg): super (). Method (arg) 

Here C &derived class, B &base class, method &a user-defined function with an argument arg .

As you can see, the line is super (). method (arg) super (C, self). method (arg) is actually equivalent to super (C, self) .method (arg) in Python 3.x. This is prohibited in Python 2.x. Therefore, using super is difficult there.

Using super ()

Consider the example below.

 class Demo: a = 0 b = 0 c = 0 def __init __ (self, A, B, C): self.a = A self.b = B self.c = C def display (self): print (self. a, self.b, self.c) cl ass Newdemo (Demo): d = 0 def __init __ (self, A, B, C, D): self.a = A self.b = B self.c = C self .d = D def display (self): print (self.a, self.b, self.c, self.d) B1 = Demo (100,200,300) print ("Contents of Base Class:") B1.display () D1 = Newdemo (10,20,30,40) print ("C ontents of Derived Class:") D1.display () 

Output:

output < / p>

In the above example, classes derived from the base class Demo were not implemented efficiently or reliably. 

The Newdemo derived class explicitly initializes the values ​​of the fields A, B, and C of the base class. The same code duplication is found when initializing the same fields in the base class, including Demo .



This process is ineffective. This means that the subclass must be given access to the members of the superclass.

Therefore, whenever a subclass needs to access its immediate superclass, super is passed to super .

Super () to call the superclass constructor

Now apply the super () method to the above example.

 class Demo: a = 0 b = 0 c = 0 def __init __ (self, A, B, C): self.a = A self.b = B self. c = C def display (self): print (self.a, self.b, self.c) class Newdemo (Demo): d = 0 def __init __ (self, A, B, C, D): self.d = D super () .__ init __ (A, B, C) #super to call super Class #The __init __ () Method def display (self): print (self.a, self.b, self.c, self.d) B1 = Demo (12,34,56) print ("Contents of Base Class:") B1.display () D1 = Newdemo (11,22,33,44) print ("Contents of Derived Class:") D1. display () 

Output:

 example super

Here the Newdemo derived class calls super ( ) with arguments a, b and c. This calls the __init__ constructor of the base class __init__ , i.e.  Demo . This initializes the values ​​a, b and c. Therefore, the Newdemo class no longer initializes the values ​​themselves.

Using super in Python 2.x

Syntax for calling the constructor super in Python 2.x is given below.

 super (Derived_Class_Name, self) .__ init __ (Parameters_of_Super_Class_Constructor) 

Therefore, we we need to make some minor changes to the above example if we want to use it in Python 2.

First, we need to put the object in the base class as shown below. < / p>

 class Demo (object): ... # other statements 

And secondly, go through Newdemo < / code> and self at the place where the superclass was called. Like it.

 super (Newdemo, self) .__ init __ (A, B, C) #super to call super Class ... #The __init__ () Method 

Why super () is needed

In the case of single inheritance with parent and child classes, super is used to implicitly reference the parent class without its explicit name ... This makes the code more efficient, maintainable, and reliable in nature.

Further, for multi-level inheritance, super can be used to implicitly reference the immediate superclass. This again makes the code easier to understand and easier to maintain.



Get Solution for free from DataCamp guru