Python Constructors

Python Programming Training Certification

Flexible Hours

100 Assignments

Instructor Led online Training

50 LMS Access

24X7 Support

100% Skill Level

Enquire Now

4.9 out of 1000+ Ratings
Best Python Institute for Learning Python Course & Training, Live Project Training in Python with Django, Data Science and AI, Interview Assistance, Expert Coaching Trainers. Python Certification & Interview Assistance! Get free demo now!

Course Overview

Python is one of the world’s top programming languages used today and Python training has become the most popular training across individuals. Training Basket’s Python Training & Certification course covers basic and advanced Python concepts and how to apply them in real-world applications.Python is a flexible and powerful open-source language that is easy to learn and consists of powerful libraries for data analysis and manipulation. Our Python training course content is curated by experts as per the standard Industry curriculum. The curriculum, coding challenges and real-life problems cover data operations in Python, strings, conditional statements, error handling, shell scripting, web scraping and the commonly used Python web framework Django. Take this Python training and certification course and become job-ready now.

Python Constructors

A constructor is a special type of method (function) which is used to initialize the instance members of the class.

In C++ or Java, the constructor has the same name as its class, but it treats constructor differently in Python. It is used to create an object.

Constructors can be of two types.

  1. Parameterized Constructor
  2. Non-parameterized Constructor

Constructor definition is executed when we create the object of this class. Constructors also verify that there are enough resources for the object to perform any start-up task.

Creating the constructor in python

In Python, the method the __init__() simulates the constructor of the class. This method is called when the class is instantiated. It accepts the self-keyword as a first argument which allows accessing the attributes or method of the class.

We can pass any number of arguments at the time of creating the class object, depending upon the __init__() definition. It is mostly used to initialize the class attributes. Every class must have a constructor, even if it simply relies on the default constructor.

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


    # accessing display() method to print employee 2 information


  • ID: 101
    Name: John
    ID: 102
    Name: David

Counting the number of objects of a class

The constructor is called automatically when we create the object of the class. Consider the following example.


  • class Student:
    count = 0
    def __init__(self):
    Student.count = Student.count + 1
    print(“The number of students:”,Student.count)


  • The number of students: 3

Python Non-Parameterized Constructor

The non-parameterized constructor uses when we do not want to manipulate the value or the constructor that has only self as an argument. Consider the following example.


  • class Student:
    # Constructor – non parameterized
    def __init__(self):
    print(“This is non parametrized constructor”)
    def show(self,name):
    student = Student()“John”)

Python Parameterized Constructor

The parameterized constructor has multiple parameters along with the self. Consider the following example.


  • class Student:
    # Constructor – parameterized
    def __init__(self, name):
    print(“This is parametrized constructor”) = name
    def show(self):
    student = Student(“John”)


  • This is parametrized constructor
    Hello John

Python Default Constructor

When we do not include the constructor in the class or forget to declare it, then that becomes the default constructor. It does not perform any task but initializes the objects. Consider the following example.


  • class Student:
    roll_num = 101
    name = “Joseph”

    def display(self):

    st = Student()


  • 101 Joseph

More than One Constructor in Single class

Let’s have a look at another scenario, what happen if we declare the two same constructors in the class.


  • class Student:
    def __init__(self):
    print(“The First Constructor”)
    def __init__(self):
    print(“The second contructor”)

    st = Student()


  • The Second Constructor

In the above code, the object st called the second constructor whereas both have the same configuration. The first method is not accessible by the st object. Internally, the object of the class will always call the last constructor if the class has multiple constructors.

Note: The constructor overloading is not allowed in Python.

Python built-in class functions

The built-in functions defined in the class are described in the following table.

SN Function Description
1 getattr(obj,name,default) It is used to access the attribute of the object.
2 setattr(obj, name,value) It is used to set a particular value to the specific attribute of an object.
3 delattr(obj, name) It is used to delete a specific attribute.
4 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
    print(getattr(s, ‘name’))

    # reset the value of attribute age to 23
    setattr(s, “age”, 23)

    # prints the modified value of age
    print(getattr(s, ‘age’))

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

    print(hasattr(s, ‘id’))
    # deletes the attribute age
    delattr(s, ‘age’)

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


  • John
    AttributeError: ‘Student’ object has no attribute ‘age’

Built-in class attributes

Along with the other attributes, a Python class also contains some built-in class attributes which provide information about the class.

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

SN Attribute Description
1 __dict__ It provides the dictionary containing the information about the class namespace.
2 __doc__ It contains a string which has the class documentation
3 __name__ It is used to access the class name.
4 __module__ It is used to access the module in which, this class is defined.
5 __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)


  • None
    {‘name’: ‘John’, ‘id’: 101, ‘age’: 22}

Recently Trained Students

Jessica Biel

– Infosys

My instructor had sound Knowledge and used to puts a lot of effort that made the course as simple and easy as possible. I was aiming for with the help of the ZebLearn Online training imparted to me by this organization.

Richard Harris


I got my training from Zeblearn in the Python Certification Training, I would like to say that say he is one of the best trainers. He has not even trained me but also motivated me to explore more and the way he executed the project, in the end, was mind-blowing.

Candidate’s Journey During Our Training Program

Card image cap

Expert’s Advice & Selection of Module

Choosing the right type of module for the training is half the battle & Our Team of experts will help & guide you.

Card image cap

Get Trained

Get Trained & Learn End to End Implementation from our Expert Trainer who are working on the same domain.

Card image cap

Work on Projects

We Do make our student’s work on multiple case studies , scenario based tasks & projects in order to provide real-time exposure to them.

Card image cap


We have a dedicated placement cell in order to provide placement assistance & relevant interviews to our candididates till selection

Placement Partner


Leave your details


Download Course Content