MOCKSTACKS
EN
Questions And Answers

More Tutorials









Python Super() and Overriding In Classes


As you people might have noticed that we came across super and overriding keywords a lot of time in a few of our previous tutorials but did not get into its detailed working. The reason for that was to give you an idea of concepts leading to the use of super() and overriding first. In this tutorial, we are going to discuss it in detail, using the single inheritance as an example.

Yet being different concepts, they are often used together. The need for superclass arises when overriding is being done at a certain point in our code. Overriding is an essential part of object-oriented programming since it makes the inheritance utilize its full power. Overriding avoids duplication of code, and at the same time, enhances or customizes the part of it. It is a part of the inheritance mechanism. Let us get a description of overriding first so that we can dive into the concept of super() later.

How to override class methods in Python?


Overriding occurs when a derived class or child class has the same method that has already been defined in the base or parent class. When called, the same methods with the same name and number of parameters, the interpreter checks for the method first in a child class and runs it ignoring the method in the parent class because it is already overridden. In the case of instance variables, the case is a little different. When the method is called, the program will look for any instance variable having the same name as the one that is called in the child, then in the parent, and after that, it comes again into child class if not found.

Where does super() fit in all this?


When we want to call an already overridden method, then the use of the super function comes in. It is a built-in function, so no requirement of any module import statement. What super does is it allows us to use the method of our superclass, which in the case of inheritance is the parent class. Syntax of using super() is given below:

class Parent_Class(object):
      def __init__(self):
            pass

class Child_Class(Parent_Class):
     def __init__(self):
           super().__init__()

super() returns a temporary object of the superclass that then allows you to call that superclass’s methods. The primary use case of super() is to extend the functionality of the inherited method.

We have discussed earlier that in the case of method overriding, the previous method could not be called, but super makes an exception, and thus we can partially or completely use the method of the parent class too. We can even use super() to call only a specific variable we used in our overridden method. Calling the superclass-built methods with super() saves us from rewriting those methods in our subclass and allows us to swap out superclasses with minimal code changes.

Example


class A:
    classvar1 = "I am a class variable in class A"
    def __init__(self):
        self.var1 = "I am inside class A's constructor"
        self.classvar1 = "Instance var in class A"
        self.special = "Special"

class B(A):
    classvar1 = "I am in class B"

    def __init__(self):
        self.var1 = "I am inside class B's constructor"
        self.classvar1 = "Instance var in class B"
        # super().__init__()
        # print(super().classvar1)


a = A()
b = B()

print(b.special, b.var1, b.classvar1)


Conclusion

In this page (written and validated by ) you learned about Python Super() and Overriding In Classes . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python Operator Overloading and Dunder Methods.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.