__new__ and __init__
standardintermediateName(...) actually runs two steps: __new__(cls, ...) creates and returns a fresh, empty object, then __init__(self, ...) sets up its attributes. __new__ is rarely overridden — almost all classes only ever touch __init__.
Think of it as
__new__ is the factory floor that stamps out a blank object; __init__ is the technician who fills it in afterward. Overriding __init__ is like changing what the technician does to an object already on the bench — overriding __new__ is like changing the stamping machine itself, needed only when the blank object has to be built differently (an immutable type, a singleton) before any setup can even begin.
What we're doing: Override __new__ to make a class a singleton — every call to Name() after the first returns the SAME object, something __init__ alone cannot do.
- 4
- __new__ runs before __init__ — this is where the singleton check has to happen, since __init__ runs every time regardless.
- 5
- cls._instance is only created on the first call — every later call returns that same stored object.
- 9
- __init__ still runs on every call (b = Config("second") triggers it too), which is why value is only set when explicitly passed.
True
secondWhy this works: a is b is True because __new__ returned the exact same cls._instance both times — Config("second") never actually creates a new object, since cls._instance was already set from the first call. __init__ still runs on the second call too (that part is unavoidable — Python calls __init__ whenever __new__ returns a cls instance), which is why b.value ends up "second": the singleton's data can still be overwritten by a later call, even though the object itself is not new.
Overriding __new__ but forgetting it must return an instance of cls
Wrong
Better
What you see: b is None, and __init__ never printed anything — no error is raised, which makes this a silent, confusing bug rather than a loud one.
Why: __new__ implicitly returns None if nothing is returned, exactly like any other function — and Python only calls __init__ when __new__ returns an instance of cls specifically. Returning None means construction quietly produces None instead of a Broken object, with no exception anywhere to point at the mistake.
- __new__(cls) — creates a blank object
- __init__(self) — sets up its attributes
- Name(...) — returns the finished object
The two-step construction pipeline
Together
Remember: Name(...) is two steps: __new__ creates the object, __init__ sets it up. Override __new__ only for singletons or immutable-type subclassing.
See also: constructors · class methods · static methods

