Thanks to visit codestin.com
Credit goes to flexiple.com

Flexiple Logo
  1. Home
  2. Blogs
  3. Python
  4. Python constructors - How to use them?

Python constructors - How to use them?

Author image

Rajat Gupta

Software Developer

Published on Tue Apr 26 2022

In Python, in order to create and initialize an object of a class, you need a special method and that special method (function) is called constructors. Every class has a constructor, but it is not required to explicitly define it. In this tutorial, we will read about Python constructors and how to use them.

What are Constructors in Python?

Constructors are generally used for instantiating an object of a class. The main objective of the constructors is to assign values to the data members of a class when an object of the class is created.
Constructors are always called when an object is created and are simulated by the __init__() method. It accepts the self-keyword, which refers to itself (the object), as the first argument, allowing access to the attributes or methods of the class.

Syntax:

def __init__(self): 

Input:

class Student:
  def __init__(self, name, roll_no):
    self.name = name
    self.roll_no = roll_no

  def display(self):
    print("Roll No.: %d \nName: %s" % (self.roll_no, self.name))

# Creating object of the class
stud1 = Student("Alex", 34)
stud2 = Student("Mark", 67)

stud1.display()
stud2.display()

We can pass any number of arguments at the time of creating the class object, based on the __init__() definition. Remember, every class must have a constructor, even if it only relies on the default constructor.

Output:

Roll No.: 34
Name: Alex
Roll No.: 67
Name: Mark

In C++ or Java, the constructor has the same name as its class but in Python, it is not the same.

Types of Python Constructors

There are two different types of constructors in Python:

Parameterized Constructor

The parameterized constructor has multiple parameters along with the self. It takes its first argument as a reference to the instance being constructed, known as self, and the rest of the arguments are provided by the programmer.

Input:

class Student:
  # Parameterized constructor
  def __init__(self, name, roll_no):
    self.name = name
    self.roll_no = roll_no

  def display(self):
    print("Roll No.: %d \nName: %s" % (self.roll_no, self.name))

# Creating object of the class
stud1 = Student("Navya", 34)
stud2 = Student("Mia", 67)

stud1.display()
stud2.display()

In the above example, a parameterized constructor is defined which takes two parameters.

Output:

Roll No.: 34
Name: Navya
Roll No.: 67
Name: Mia

Non-parameterized Constructor

The non-parameterized constructor is used when we do not want to manipulate the value or when the constructor has only self as an argument. Its definition has only one argument, which is a reference to the instance being constructed.

Input:

class Student:
  # No-argument constructor
  def __init__(self):
    self.name = "Mark"
    self.roll_no = 67

  def display(self):
    print('Roll No.:', self.roll_no, '\nName:', self.name)

# Creating object of the class
stud = Student()

stud.display()

As you can see, there is no argument sent to a constructor while creating an object.

Output:

Roll No.: 67
Name: Mark

Closing thoughts

Remember, if you create four objects, the class constructor is called four times too. Just as Python has constructors, it also has destructors which are used to destroy the object of a class. One can learn about more Python concepts here.

Related Blogs

Browse Flexiple's talent pool

Explore our network of top tech talent. Find the perfect match for your dream team.