Python constructors - How to use them?
Tags: python, programming, beginners, tutorial
Published: 2022-04-26 08:02:07

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.

## Table of contents

1.  [Python constructors - definition](#what-are-constructors-in-python)
2.  [Types of constructors](#types-of-python-constructors)
3.  [Closing thoughts](#closing-thoughts)

## What are Constructors in Python?

Constructors are generally used for instantiating an object of a class. The main objective of the constructors is the 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 is simulated by the __init__() method. It accepts the self-keyword, which refers to itself (the object), as a first argument which allows accessing the attributes or method 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 tie of creating the class object, based upon the __init__() definition. Remember, every class must have a constructor, even if it only relies on the default constructor.

<span id="section2"></span>

#### 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 uses when we do not want to manipulate the value or the constructor that 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, \n'Name:', 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.

<span id="section3"></span>

#### 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](https://pythonprogramminglanguage.com/destructor/) which are used to destroy the object of a class. One can learn about more Python concepts [here](https://flexiple.com/python/open-python).