Home » The Impact of Constructors on Python Programming

The Impact of Constructors on Python Programming

constructors in python

by shweta insideaiml
0 comment

A class’s constructor is a special function that is invoked at object creation time. Constructors in Python are typically used to populate the class’s data members with default values. It is impossible for it to give any result other than null.

Constructors are used primarily to instantiate objects. Constructors are methods of a class that are invoked when an object of that class is formed in order to populate (assign values to) its data members. When a Python object is created, it calls the __init__() method, also known as the constructor.


Let’s have a look at the constructor’s usage.


Syntax of Python Constructor

def __init__(self):

# initializations


Init is one of the reserved functions in Python. In object-oriented programming, it is known as a constructor.

Rules of Python Constructor

Like all Python functions, it begins with the def keyword.

In its place comes the term “init,” which is surrounded by a pair of double-underscore brackets (__init__()).

It accepts a keyword parameter named self, which, when used to assign values to variables, is a pointer to the active instance of the class. When the constructor is used, it is created and passed on to __init__() either explicitly or indirectly.


Types of Constructors in Python

Parameterized Constructor

Non-Parameterized Constructor

            Default Constructor


1. Parameterized Constructor in Python


When the constructor accepts arguments along with itself, it is known as a parameterized constructor.

These arguments can be used inside the class to assign values to the data members. Let’s see an example:


class Family:  

    # Constructor – parameterized  


    def __init__(self, count):  

        print(“This is parametrized constructor”)  

        self.members = count

    def show(self):  

        print(“No. of members is”, self.members)  


object = Family(10)  





This is parameterized constructor

No. of members is 10




A new instance of the Family class is made. It contains a member variable.

A parameter (in this case, the number 10) is supplied during the object’s creation.

The constructor will use this value (10 in the preceding example) while creating the object.

The variable count is set to 10, and the self-members are also given the value 10.

The class’s self-members can be utilized for internal data printing.


2. Non-Parameterized Constructor in Python


When the constructor doesn’t accept any arguments from the object and has only one argument, self, in the constructor, it is known as a non-parameterized constructor.

This can be used to re-assign a value inside the constructor. Let’s see an example:



class Fruits:

    favourite = “Apple”


    # non-parameterized constructor

    def __init__(self):

        self.favourite = “Orange”


    # a method

    def show(self):



# Creating an object of the class

obj = Fruits()


# calling the instance method using the object obj








In the preceding code snippet, our favorite fruit is set as an internal initial value of “orange” in the class. However, we’d like to make the modification right after the object is created.

This can be achieved by reassigning the value to the constructor. No arguments other than self are accepted by this kind of constructor. It can be used to see if our value has changed and to print it out.


3. Default Constructor in Python


When you do not write the constructor in the class you create, Python itself creates a constructor during the compilation of the program.

It generates an empty constructor that has no code in it. Let’s see an example:




class Assignments:

    check= “not done”

    # a method

    def is_done(self):



# Creating an object of the class

obj = Assignments()


# calling the instance method using the object obj





not done



In the given scenario, we are determining if our homework is complete. We instantiate a Class object without providing a custom constructor.

If we do not supply a constructor to initialize the instance variables, Python will create one for us. It has no effect.



When a new instance of a class object is created, a method known as the constructor is invoked.

The generation of the constructors in python is dependent on the programmer; if the programmer does not create them, Python will build the default constructors automatically.

You may also like

Colorful Live News Free Logo (1)

Soledad is the Best Newspaper and Magazine WordPress Theme with tons of options and demos ready to import. This theme is perfect for blogs and excellent for online stores, news, magazine or review sites.

Editors' Picks

Latest Posts

© 2022 Read News Hourly

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
Update Required Flash plugin