How to use super() with `__init__()` method in Python?

In this article, we will learn to use super() with __init__() method in Python. But before lets learn about super() and __init__() method.

Table Of Contents

What is __init__() method ?

The __init__() is a special method in python programming language which is also known as a constructor. The __init__() is the core of Object-Oriented Programming concept, and used in other programming languages like C++ and Java. The __init__() function is always called, when an object is created. When an object is created, space is allocated in memory and __init__() is called. So address of object is passed to __init__().

EXAMPLE CODE:

class Players:
    #__init__() method initialized the object.
    def __init__(self,name,match,runs):
        # self stores the address and binds the attributes with the given argument.
        self.name = name
        self.match = match
        self.runs = runs
    def scoreCard(self):
        print(self.name, 'has scored', self.runs, 'runs in', self.match, 'matches.')

# creating an instance of Players class.
p = Players('Virat',16,971)

p.scoreCard()

OUTPUT:

Advertisements
Virat has scored 971 runs in 16 matches.

In this example, we have a class Player, and it has a constructor. While creating the object of Player, we passed the name, runs and match as arguments. These arguments gets to the __init__() method which initializes the object. This way we can create scorecard for other players by just passing different arguments/data.

Using ‘ super().__init__() ‘

super() function in Python comes from a very important OOP(Object-Oriented Programming) Concept called Inheritance. The super() function provides access to methods of Parent or Sibling class. This function returns objects in parent class. Also super() function is used with __init__() method.

SYNTAX:

super().__init__() `

Now see the Example code below then we will discuss super() with __init__().

CODE :

class Players:
    #__init__() method initialized the object.
    def __init__(self,name,match,runs):
        # self stores the address and binds the attributes with the given argument.
        self.name = name
        self.match = match
        self.runs = runs
    def scoreCard(self):
        print(self.name,'has scored',self.runs,'runs in',self.match, 'matches.')


class Bowlers(Players):
    def __init__(self,name,match,runs,wicket):
        self.wicket = wicket
        super().__init__(name,match,runs)
    def stats(self):
        print(self.name, 'took', self.wicket, 'in', self.match, 'matches.')


b = Bowlers('Bumrah', 29, 102, 43)

b.stats()

OUTPUT :

Bumrah took 43 in 29 matches.

So In the code and output above you can see we have two classes, one named as Players with name, matches and runs as arguments. In other Class named as Bowlers we want to include an extra argument which is wicket, so instead of binding everything with self keyword we simply inherited some bindings from the Parent class which is class Players here. For this in Python we use super().__init__().

So, when we create a new Class with something derived from the parent class, base class’s __init__() is called from the derived class’s __init__() function. The syntax used for calling a base class constructor is super().__init__(). Derived class object contains all the base class data. Also the derived class member functions can access base class members.

Summary

So in this Python tutorial How to use super() with __init__() method in Python, also we learned about super() function and __init__() method. Since, super() function is used for inheritance and __init__() is used to initialize a new Class argument. So, using super(),init() can be very useful in Inheritance.

Make sure to practice with the above samples, to have a better understanding of this concept. Thanks.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top