Python Tutorial

Python Constructors and Their Types

Table of Contents

  • What are Constructors in Python?
  • Syntax of constructor declaration
  • Types of Python Constructors

What are Constructors in Python?

Constructors are commonly employed to create new objects. Constructors are responsible for initialising (assigning values) the data members of a class when an object is formed. The function Object() { [native code] } is called the __init__() function in Python, and it is invoked whenever an object is created.

Syntax of constructor declaration

def __init__(self):

    # body of the constructor

Types of Python Constructors

1. Default constructor

The default constructor doesn’t accept any arguments. It contains only one argument, which is a reference to the instance that is being built.

2. Parameterized constructor

A parameterized constructor is a constructor with parameters. The initial parameter is a reference to the instance being built, which is called self, and the rest of the arguments are supplied by the programmer.

Did you find this article helpful?