Constructor in Python

Video Tutorial
FREE
Class Constructor thumbnail
This video belongs to
Python Course for Beginners With Certification: Mastering the Essentials
16 modules
Certificate
Topics Covered

Overview

In Python, a constructor is a special method within a class designated to initialize new class instances. When an object of the class is created, the constructor is automatically invoked, setting up the object's initial state.

What is a Constructor?

A constructor is a unique function that gets called automatically when an object of a class is created. 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

Within the 'init' method, you can define the object's initial state by assigning values to the object's properties or performing other necessary startup procedures. The self parameter refers to the current class instance to access class attributes and methods.

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


Parameterized Constructor in Python

In Python, a parameterized constructor is a type of constructor that takes additional arguments besides the standard self reference. These arguments are used to initialize the object's attributes or perform other operations when the object is created.

Here's an example to illustrate this concept:

Code:

Output:

Explanation:

  • The Family class has a parameterized constructor that accepts one argument (count).
  • An instance of Family is created with the parameter 10. This value is passed to the __init__ method.
  • Inside the constructor, the passed value (10) is assigned to the instance attribute self.members.
  • The show method then accesses this attribute and prints the number of family members.

2. Non-Parameterized Constructor in Python


Non-Parameterized Constructor in Python

A non-parameterized constructor in Python is a constructor that does not accept any arguments except the mandatory self. This type of constructor is used for initializing class members with default values or performing standard initialization tasks that do not require external inputs.

Here's an example to illustrate a non-parameterized constructor:

Code:

Output:

Explanation:

  • In this example, the Fruits class has a class variable favourite initially set to "Apple".
  • The non-parameterized constructor __init__ changes the favourite attribute to "Orange".
  • When the Fruits class object is created, the constructor is called, and favourite is set to "Orange".
  • The show method is then called on the object, which prints the updated favourite fruit.

3. Default Constructor in Python

When a class is defined without an explicit constructor in Python, Python automatically provides a default constructor. This default constructor is a basic, non-parameterized constructor that performs no specific task or initialization beyond the basic object creation.

Here's an example to illustrate the concept of a default constructor:

Example:

Output:

Explanation:

  • The class Assignments is defined without an explicit constructor (__init__ method).
  • The class has an instance variable check, initialized to "not done".
  • When an object obj of the class Assignments is created, Python automatically invokes the default constructor.
  • This default constructor does not modify any attributes or perform actions other than creating the object.
  • The is_done method is then called on the object, which prints the current value of check.

Become a Python champion with our certification course. Start your journey towards excellence today!

Conclusion

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