Python Constructor: Parameterized, Non parameterized Constructors

A constructor is nothing but a specialized method that initializes the data members of a class. It is basically used to instantiate the object of a class. In other programming languages like C++ and Java, the constructor is created by using the same name of the class and the constructor has no return type. In the case of Python, it is a little bit different from other object-oriented programming languages. The __ init( )__ method is used to call the constructor of a class in Python language. There are three types of constructors and they are:

  • Default constructor
  • Parameterized constructor
  • Non parameterized constructor

Default constructor:

This constructor does not accept any other parameter as an argument. The default constructor is basically used when the programmer does not use any constructors in the class, it initializes the objects. It is automatically done by the Python language.

Parameterized constructor:

The parameterized constructor is nothing but a constructor with multiple parameters. Here also the __init( )__ method is used to call the constructor. But in this type of constructor, the first argument is the self keyword. After passing the self keyword as the first argument, the programmer can pass any number of arguments through the __init( )__ method.

Syntax:

class Class_name:
     def __init( )__ ( self, argument1, argument2, …. argument n ):
         Statement of block

Non parameterized constructor:

The __init( )__ method is used to call the constructor of a class and this method is always called when the object of a class is created. This constructor accepts only the self keyword as an argument. 

Syntax:

class Class_name:
    def __init( )__ ( self ):
        Statement of block

Creating the constructor in python:

Python uses the __ init ( )__ method to call the constructor. Any number of arguments can be passed through the method. In case of default constructor, the only self keyword is passed as an argument. In the case of a parameterized constructor, the self keyword must be passed as the first argument. Then multiple arguments can be passed through the method.  

Sample code:

class Student: # creation of class
    def __init( )__ ( self, s_name, s_id ): # parameterized constructor
          self. s_name = s_name # data members
          self. s_id = s_id # data members
    def  create ( self ): # create method
           print ("Student ID: %d" % (self. s_id) )
           print ("Student Name: %s" % (self. s_name) )
s1 = Student ("Jack", 1122 ) # passing arguments at the time of object creation
s1 = Student ("Smith", 1133 )
s1. create ( )
s2. create ( )

Counting Objects:

Using the constructor, the number of objects got created in a class can be counted. The default constructor is automatically called when the object of a class is created.

Sample Code:

class Teacher: # creation of class
     c = 0
     def __init()__ ( self ): # parameterized constructor
          Teacher. c = Teacher. c + 1 # increasing the count data member
t1 = Teacher() # creating first object of Teacher class
t2 = Teacher() # creating second object of Teacher class
t3 = Teacher() # creating third object of Teacher class
print (Teacher. c) # it prints the value 3 as three objects got created for Teacher class

More than one constructor with the same name:

If a class has multiple constructors with different names, it does not create any problem at the time of object creation. In Python, multiple constructors can be created by using the same name. But the object calls the last constructor by default in Python.