Constructor in Python

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Video Tutorial

Class Constructor

Overview

A constructor is a special method in a class used to create and initialize an object of a class. There are different types of constructors in Python programming language. Constructor is invoked automatically when an object of a class is created.

What is a Constructor?

A constructor is a unique function that gets called automatically when an object is created of a class. The main purpose of a constructor is to initialize or assign values to the data members of that class. It cannot return any value other than none.

Let's see how to use the constructor.

Syntax of Python Constructor

init is one of the reserved functions in Python. In Object Oriented Programming, it is known as a constructor.

Rules of Python Constructor

  • It starts with the def keyword, like all other functions in Python.
  • It is followed by the word init, which is prefixed and suffixed with double underscores with a pair of brackets, i.e., __init__().
  • It takes an argument called self, assigning values to the variables.

Self is a reference to the current instance of the class. It is created and passed automatically/implicitly to the __init__() when the constructor is called.

Types of Constructors in Python

  1. Parameterized Constructor
  2. Non-Parameterized Constructor
  3. Default Constructor

1. Parameterized Constructor in Python

When the constructor accepts arguments along with self, it is known as parameterized constructor.

These arguments can be used inside the class to assign the values to the data members. Let's see an example:

Code:

Output:

Explanation

  • An object of the class Family is created. It has a variable known as members.
  • When the object is created, a parameter (here it is 10) is passed as arguments.
  • This parameter (10 as in the above example) is taken up by the constructor as the object is created.
  • The number 10 is assigned to the variable count, which is further assigned to self.members.
  • The self.members can be used within the class to print the data.

2. Non-Parameterized Constructor in Python

When the constructor doesn't accept any arguments from the object and has only one argument, self, in the constructor, it is known as a non-parameterized constructor.

This can be used to re-assign a value inside the constructor. Let's see an example:

Code:

Output:

Explanation:

In the above example, we initialize our favorite fruit as Apple inside the class. But we want to change it as soon as the object gets created.

This can be done by re-assigning the value to the constructor. This type of constructor doesn't take any other argument other than self. It can be used to print and check whether our value got changed.

3. Default Constructor in Python

When you do not write the constructor in the class created, Python itself creates a constructor during the compilation of the program.

It generates an empty constructor that has no code in it. Let's see an example:

Example

Output

Explanation

In the above example, we check whether our assignment is done.

We create an object of a class where we do not write the constructor.

Python generates a constructor when we do not provide any to initialize the instance variables. It does nothing.

Conclusion

  • The constructor is a method that is called when an object is created of a class.
  • The creation of the constructor depends on the programmer, or else Python will automatically generate the default constructor.
  • It can be used in three types - Parameterized Constructor, Non-Parameterized Constructor, Default Constructor.