__new__
def __new__(cls, *args, **kwargs) object · Updated March 19, 2026 · Dunder Methods __new__ is called to create a new class instance, before __init__ runs. It receives cls as its first argument and must return an instance of the class (or a subclass). The returned instance is then passed to __init__ for initialisation, unless __new__ returns something that is not an instance of the class.
Signature
def __new__(cls, *args, **kwargs):
...
__new__ is implicitly a static method — you do not need to decorate it with @staticmethod. Its first argument is always cls, the class itself, not an instance. Any arguments passed to the constructor (e.g. MyClass(1, 2, key="val")) are forwarded as *args and **kwargs.
Parameters
| Parameter | Description |
|---|---|
cls | The class whose instance is being created. Always the first argument. |
*args | Positional arguments passed to the constructor, after cls. |
**kwargs | Keyword arguments passed to the constructor. |
Return Value
__new__ must return a new instance of the class or of a subclass. If it returns an instance of cls (or a subclass), Python automatically calls __init__ on that instance with the original *args, **kwargs. If __new__ returns an object that is not an instance of cls or a subclass, Python’s default __init__ is not called at all.
Note that returning a subclass instance is fine — Python still calls __init__ on that subclass instance. This is how the factory pattern works.
Examples
Singleton
A singleton ensures a class has only one instance. __new__ intercepts construction and returns the same object on every call:
import threading
class Singleton:
_lock = threading.Lock()
_instance = None
def __new__(cls):
if cls._instance is None:
with cls._lock:
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
a = Singleton()
b = Singleton()
assert a is b # True
The double-checked locking pattern ensures thread-safe singleton creation. The outer if avoids acquiring the lock on every call once the instance exists.
Immutable object
__new__ can set instance state before __init__ runs, preventing modification via __setattr__. Set the data attributes first, then freeze last:
class ImmutablePoint:
def __new__(cls, x, y):
instance = super().__new__(cls)
instance._x = x
instance._y = y
instance._frozen = True
return instance
def __setattr__(self, name, value):
if getattr(self, '_frozen', False):
raise AttributeError(f"'{type(self).__name__}' object is immutable")
super().__setattr__(name, value)
p = ImmutablePoint(1, 2)
print(p._x) # 1
# p._x = 3 # raises AttributeError
Factory pattern
__new__ can return a different subclass depending on the arguments, acting as a factory:
class Animal:
def __new__(cls, name, sound):
if cls is Animal:
return Dog(name, sound)
return super().__new__(cls)
class Dog(Animal):
def __init__(self, name, sound):
self.name = name
self.sound = sound
a = Animal("Rex", "woof")
print(type(a)) # <class '__main__.Dog'>
print(a.sound) # woof
Calling Animal(...) where cls is Animal returns a Dog instance instead. The caller receives a Dog without needing to know the subclass exists.
Common Gotchas
Returning a non-cls instance suppresses __init__.
class Str:
def __new__(cls, value):
return str(value) # returns a plain str, not a Str instance
s = Str(42)
print(type(s)) # <class 'str'> — __init__ was never called
This is a frequent source of silent bugs. If you need __init__ to run, __new__ must return cls or a subclass instance.
Forgetting to pass cls to object.__new__.
def __new__(cls, *args, **kwargs):
obj = object.__new__(cls) # correct: pass cls
# ...
return obj
object.__new__(cls) is the correct call. Passing no argument or the wrong type breaks instance allocation.
Using self in __new__.
There is no self at the __new__ stage — the instance does not exist yet. Use cls to refer to the class and super().__new__(cls) to allocate it.
Thread-unsafe singletons without locking.
The basic singleton pattern is not safe for multi-threaded code. Two threads can both see _instance is None and create duplicate instances. Use a lock or module-level assignment for thread-safe singletons.