Python Constructor

Python Constructor
A constructor is just a method or function, but it is of special type. The work of the constructor is to initialise the instance members of the class.

There are two types of constructors:

  1. Parameterized Constructor
  2. Non-parameterized Constructor

The constructor is executed when the object of any class is created. The constructor also checks that there are enough number of resources avalable for the object to perform any startup task.

Creating the constructor in python
In python, the function __init__ is assigned the work of the constructor of the class i.e. the __init__() function is the constructor of the class. When a class is instantiated then at that time this method is called. Any number of arguments or parameters can be passes when we create the object of the class and that will depend upon the __init__() function definition. It is mostly used to initialize the class attributes. Each and every class must have a constructor, even if it simply relies on the default constructor.

For better understanding consider the following example to initialize the Employee class attributes.

class Employee:

def __init__(self,name,id): = id; = name;

def display (self):

print("ID: %d \nName: %s"%(,

emp1 = Employee("John",101)

emp2 = Employee("David",102)

#accessing display() method to print employee 1 information emp1.display();

#accessing display() method to print employee 2 information


Example: Counting the number of objects of a class

class Student:

     count = 0

     def __init__(self):

         Student.count = Student.count + 1




print("The number of students:",Student.count)

Non-Parameterized Constructor Example in Python

class Student:

     # Constructor - non parameterized

     def __init__(self):

         print("This is non parametrized constructor")

     def show(self,name):


student = Student()"John")

Parameterized Constructor Example in Python

class Student:

     # Constructor - parameterized

     def __init__(self, name):

         print("This is parametrized constructor")
 = name

     def show(self):


student = Student("John")

In-built class functions in Python
The in-built functions defined in the class are described in the following table.
getattr(obj,name,default) It is used to access the attribute of the object.
setattr(obj, name,value) It is used to set a particular value to the specific attribute of an object.
delattr(obj, name) It is used to delete a specific attribute.
hasattr(obj, name) It returns true if the object contains some specific attribute.


class Student:

     def __init__(self,name,id,age):
 = name;
 = id;

         self.age = age

#creates the object of the class Student

s = Student("John",101,22)

#prints the attribute name of the object s


# reset the value of attribute age to 23


# prints the modified value of age


# prints true if the student contains the attribute with name id


# deletes the attribute age


# this will give an error since the attribute age has been deleted


Built-in class attributes
In Python the class also contains some of the built in attributes of the class along with other attributes that will provide information about the class.

The built in class attributes are given in the table below .

__dict__ It provides the dictionary containing the information about the class namespace.
__doc__ It contains a string which has the class documentation
__name__ It is used to access the class name.
__module__ It is used to access the module in which, this class is defined.
__bases__ It contains a tuple including all base classes.

class Student:

     def __init__(self,name,id,age):
 = name;
 = id;

         self.age = age

     def display_details(self):

         print("Name:%s, ID:%d, age:%d"%(,

s = Student("John",101,22)