Constructor using `new` method
Introduction
In Python, object creation and object initialization are intentionally separated into two distinct steps, providing a level of flexibility that many programming languages do not offer.
- Object creation is handled by the
__new__method, which is responsible for allocating memory and returning a new instance of a class. - Object initialization is handled by the
__init__method, which configures the already-created object by assigning initial values to its attributes.
This separation allows Python to give developers fine-grained control over how and when objects are created, enabling advanced use cases such as customized object creation, immutable data handling, and design patterns like the Singleton.
What is __new__?
__new__is a special method responsible for creating a new object- It is the true constructor in Python
- It is called before
__init__ - It receives the class object as its first parameter, commonly named
clsby convention - It must return an instance of the class
__new__(cls)If __new__ does not return an object, Python cannot proceed with initialization.

Sequence of Object Creation
When an object is created using:
obj = ClassName()The execution order is:
__new__(cls)→ creates the object__init__(self)→ initializes the object- Other instance methods can then be called

Basic Example Without __new__
class Abc:
def __init__(self):
print("init called")
def show(self):
print("in show")
obj1 = Abc()
obj1.show()Output:
init called
in showThis is the standard object creation flow, where Python internally calls __new__ for you.
Creating an Object Using __new__ Directly
obj2 = Abc.__new__(Abc)
obj2.show()Output:
in showExplanation:
__init__is not called- Object is created, but not initialized
- Methods that do not depend on instance variables still work
Defining a Custom __new__ Method
class Abc:
def __new__(cls):
print("Constructor called")
def __init__(self):
print("init called")
def show(self):
print("in show")
obj = Abc()
obj.show()Output:
Constructor called
AttributeError: 'NoneType' object has no attribute 'show'Why This Fails?
__new__did not return an object- Python receives
None __init__is never called- Method calls fail
Correct Way to Implement __new__
A custom __new__ must always return an object using super().__new__(cls).
class Abc:
def __new__(cls):
print("Constructor called")
return super(Abc, cls).__new__(cls)
def __init__(self):
print("init called")
def show(self):
print("in show")
obj1 = Abc()
obj1.show()Output:
Constructor called
init called
in showManually Calling __init__ with __new__
obj2 = Abc.__new__(Abc)
obj2.__init__()
obj2.show()Output:
Constructor called
init called
in showThis shows that:
__new__creates the object__init__must be called explicitly if skipped
Singleton Pattern Using __new__
- Ensures only one instance of a class exists
- Repeated object creation returns the same instance
- Achieved by storing and reusing the object inside
__new__
class Singleton:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instanceRole of super() in __new__
- Delegates object creation to the parent class
- Maintains proper behavior in inheritance chains
- Ensures correct memory allocation
return super().__new__(cls)Summary
__new__is Python’s actual constructor and runs before__init__.- It is responsible for creating and returning the object, while
__init__only initializes it. - A custom
__new__must return a valid instance; otherwise, object behavior breaks. __init__should be used for normal initialization, while__new__is reserved for advanced cases.__new__is essential for special object-creation logic, such as the Singleton pattern.
Written By: Muskan Garg
How is this guide?
Last updated on
