Advertisement
Advanced Python, Magic Methods & Operator Overloading

What is the difference between __new__ and __init__, and when would you override __new__?

Short answer

__new__(cls, ...) is a static method that actually creates and returns the new instance (allocating memory), called before __init__. __init__(self, ...) then initializes that already-created instance and returns None.

Save Python mock interview
Advertisement
AModel answer
__new__(cls, ...) is a static method that actually creates and returns the new instance (allocating memory), called before __init__. __init__(self, ...) then initializes that already-created instance and returns None. You override __new__ for cases __init__ can't handle: controlling instance creation for immutable types (like subclassing tuple or str), implementing singletons, or returning an instance of a different class.
class Singleton:
    _instance = None
    def __new__(cls, *args, **kwargs):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
        return cls._instance

a, b = Singleton(), Singleton()
print(a is b)  # True
Real-world example
A configuration manager implements __new__ to enforce a single shared instance across the entire application, ensuring all code reads the same in-memory config object.
Likely follow-ups
  • Why can't __init__ alone implement a singleton reliably?
  • How does __new__ interact with immutable types like int subclasses?
Related topics