Open In App

Constructors in Python

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
362 Likes
Like
Report

In Python, a constructor is a special method that is called automatically when an object is created from a class. Its main role is to initialize the object by setting up its attributes or state.

The method __new__ is the constructor that creates a new instance of the class while __init__ is the initializer that sets up the instance's attributes after creation. These methods work together to manage object creation and initialization.

__new__ Method

This method is responsible for creating a new instance of a class. It allocates memory and returns the new object. It is called before __init__.

class ClassName:
    def __new__(cls, parameters):
        instance = super(ClassName, cls).__new__(cls)
        return instance

To learn more, please refer to "__new__ " method

__init__ Method

This method initializes the newly created instance and is commonly used as a constructor in Python. It is called immediately after the object is created by __new__ method and is responsible for initializing attributes of the instance.

Syntax:

class ClassName:
    def __init__(self, parameters):
        self.attribute = value

Note: It is called after __new__ and does not return anything (it returns None by default).

To learn more, please refer to "__init__" method

Differences Between __init__ and __new__

__new__ method:

  • Responsible for creating a new instance of the class.
  • Rarely overridden but useful for customizing object creation and especially in singleton or immutable objects.

__init__ method:

  • Called immediately after __new__.
  • Used to initialize the created object.

Types of Constructors

Constructors can be of two types.

1. Default Constructor

A default constructor does not take any parameters other than self. It initializes the object with default attribute values.


Output
Toyota
Corolla
2020

2. Parameterized Constructor

A parameterized constructor accepts arguments to initialize the object's attributes with specific values.


Output
Honda
Civic
2022

Next Article
Article Tags :
Practice Tags :

Similar Reads