PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python » Python Object-Oriented Programming (OOP) » Constructors in Python

Constructors in Python

Updated on: August 28, 2021 | 10 Comments

Constructor is a special method used to create and initialize an object of a class. On the other hand, a destructor is used to destroy the object.

After reading this article, you will learn:

  • How to create a constructor to initialize an object in Python
  • Different types of constructors
  • Constructor overloading and chaining

Table of contents

  • What is Constructor in Python?
    • Example: Create a Constructor in Python
  • Types of Constructors
    • Default Constructor
    • Non-Parametrized Constructor
    • Parameterized Constructor
  • Constructor With Default Values
  • Self Keyword in Python
  • Constructor Overloading
  • Constructor Chaining
  • Counting the Number of objects of a Class
  • Constructor Return Value
  • Conclusion and Quick recap

What is Constructor in Python?

In object-oriented programming, A constructor is a special method used to create and initialize an object of a class. This method is defined in the class.

  • The constructor is executed automatically at the time of object creation.
  • The primary use of a constructor is to declare and initialize data member/ instance variables of a class. The constructor contains a collection of statements (i.e., instructions) that executes at the time of object creation to initialize the attributes of an object.

For example, when we execute obj = Sample(), Python gets to know that obj is an object of class Sample and calls the constructor of that class to create an object.

Note: In Python, internally, the __new__ is the method that creates the object, and __del__ method is called to destroy the object when the reference count for that object becomes zero.

In Python, Object creation is divided into two parts in Object Creation and Object initialization

  • Internally, the __new__ is the method that creates the object
  • And, using the __init__() method we can implement constructor to initialize the object.

Syntax of a constructor

def __init__(self):
    # body of the constructorCode language: Python (python)

Where,

  • def: The keyword is used to define function.
  • __init__() Method: It is a reserved method. This method gets called as soon as an object of a class is instantiated.
  • self: The first argument self refers to the current object. It binds the instance to the __init__() method. It’s usually named self to follow the naming convention.

Note: The __init__() method arguments are optional. We can define a constructor with any number of arguments.

Example: Create a Constructor in Python

In this example, we’ll create a Class Student with an instance variable student name. we’ll see how to use a constructor to initialize the student name at the time of object creation.

class Student:

    # constructor
    # initialize instance variable
    def __init__(self, name):
        print('Inside Constructor')
        self.name = name
        print('All variables initialized')

    # instance Method
    def show(self):
        print('Hello, my name is', self.name)


# create object using constructor
s1 = Student('Emma')
s1.show()Code language: Python (python)

Output

Inside Constructor
All variables initialized

Hello, my name is Emma
  • In the above example, an object s1 is created using the constructor
  • While creating a Student object name is passed as an argument to the __init__() method to initialize the object.
  • Similarly, various objects of the Student class can be created by passing different names as arguments.