The Story That Explains These Three Ideas
Step 1 — build the chassis. The factory line rolls out a steel body. It's a real car now, but empty inside. That's
__new__ — it creates
the object.Step 2 — customise it. Painters add colour, workers install seats and dashboard. That's
__init__ — it initialises the freshly-created object.So every time you write
Car("Honda"), Python runs __new__ to build
the empty car, then __init__ to fill in "Honda" as the brand.Two special twists follow. First: some things exist only once — the Sun, the company CEO, the app configuration. Ask for one a hundred times and you get the same one every time. That's the Singleton pattern — you customise
__new__ to always hand back the same instance. Second: some objects act like verbs — you
press them and they do something. A horn is an object, but horn() makes a sound.
That's __call__ — it makes your instance itself callable, just like
a function.
__new__ creates the object. __init__
fills it in. __call__ lets the object be called like a function.
Combine them and you get singletons (one instance, always reused) and
callable objects (verbs in class form). Three tools, one connected family.
__new__ vs __init__ — The Full Lifecycle
When you write Person("Alice", 30), two methods run in order. Almost every Python
class only needs __init__ — Python's default __new__ handles the
creation invisibly. Understanding both is what unlocks singletons, immutable subclasses of
built-in types, and metaclasses.
Person("Alice", 30)Person.__new__(cls, "Alice", 30) — this creates a bare, empty Person object and returns it.
Person.__init__(new_object, "Alice", 30) — this fills in the attributes on the object __new__ just returned.
__new__ receives the class (cls) — it doesn't have a self yet. __init__ receives the object (self) that __new__ just created.
class Person:
def __new__(cls, *args, **kwargs):
print(f" [__new__] creating a new Person object")
instance = super().__new__(cls) # let object create it for us
return instance # MUST return the new object
def __init__(self, name, age):
print(f" [__init__] filling in name={name!r}, age={age}")
self.name = name
self.age = age
print("Creating Alice:")
alice = Person("Alice", 30)
print()
print("Creating Bob:")
bob = Person("Bob", 25)
Animated Diagram — The Two-Step Object Creation
Follow Person("Alice", 30). Python fires __new__ first — it produces
a bare, unnamed object. Then it fires __init__ on that same object — filling in
name and age. The finished object is what your variable receives.
Every Person(...) call runs this two-step pipeline. __new__ builds the empty shell; __init__ customises it.
When Do You Actually Override __new__?
99% of Python classes never touch __new__. The default handles it. You reach for it
only when you need to control which object is returned, or subclass an immutable built-in
type.
__new__.
str, int, tuple)
means __init__ can't set state — the object is already frozen. Do it in
__new__ instead.
int class does this for small numbers (integer caching).
__new__ is a static-like method whose first argument is
cls (the class), not self. It must return
an instance. If it returns something that's not an instance of cls,
Python won't call __init__ afterwards.
__call__ — Making an Instance Callable
Functions are callable — len(), print(), your own
calculate(). Instances are usually not callable. Defining
__call__ changes that: your instance can now be invoked with
() just like a function.
class Multiplier:
def __init__(self, factor):
self.factor = factor
# ── This makes the instance callable ──
def __call__(self, x):
return x * self.factor
double = Multiplier(2)
triple = Multiplier(3)
# Instances now behave like functions
print(double(10)) # 20 ← calls double.__call__(10)
print(triple(10)) # 30 ← calls triple.__call__(10)
# They can even be passed anywhere a function is expected
print(list(map(double, [1, 2, 3]))) # [2, 4, 6]
# They remain OBJECTS — you can inspect and mutate state
print(double.factor) # 2
double.factor = 5
print(double(10)) # 50 ← same instance, new behaviour
Animated Diagram — obj() Becomes obj.__call__()
Calling an instance with () is really Python calling its __call__ method. Same syntax as functions, but you keep instance state.
Why Callable Classes Instead of Just Functions?
| No memory between calls |
| Configuration passed every time |
| Fine for stateless operations |
def multiply(x, factor): |
| Carries state across calls |
| Configured once at construction |
| Reads like a function, behaves like an object |
Multiplier(2) — then reuse forever |
Common patterns that benefit from __call__:
- Configured functions — a validator with rules bound at construction (
Validator(min_length=8)), then called asvalidator("input") - Counters and accumulators — instance carries the running total between calls
- Decorators as classes —
@Retry(max_attempts=3)— the decorator's__call__wraps the function - Pipelines / transforms —
Pipeline(steps)thenpipeline(data) - Fluent APIs — chainable calls that also carry inspectable state
The Singleton Pattern — One Instance, Reused Forever
A singleton is a class that only ever produces one instance.
No matter how many times you call Config(), you get the same
Config object every time. Perfect for app-wide state — configuration, database
connections, logging, caches.
class Config:
_instance = None # class variable — the "one and only" slot
def __new__(cls, *args, **kwargs):
if cls._instance is None:
print(" [__new__] first call — creating THE instance")
cls._instance = super().__new__(cls)
else:
print(" [__new__] already exists — returning the same instance")
return cls._instance
def __init__(self, filename=None):
# NOTE: __init__ runs on EVERY Config(...) call — beware of clobbering
if filename is not None:
self.filename = filename
print("First call:")
a = Config("app.ini")
print("\nSecond call:")
b = Config()
print("\nThird call:")
c = Config("override.ini")
print()
print("a is b:", a is b) # True — literally the same object
print("b is c:", b is c) # True
print("a.filename:", a.filename) # override.ini (the last __init__ clobbered it)
__init__ Gotcha
__init__ runs on every Config(...) call — even the ones
where __new__ returned the existing instance. That means calling
Config("override.ini") a second time overwrites the filename set the
first time. Guard against re-init explicitly if you don't want that (see the next section
for the "initialized flag" pattern).
Animated Diagram — All Three Calls Return the Same Object
Three different calls (a, b, c), one memory address. __new__ checks the slot, hands back the existing object, skips creation.
A Safer Singleton — Guard __init__ Too
Because __init__ runs on every construction call, you often want a flag to run the
actual setup only once. This is the "initialised once" pattern used in most real singletons.
class ConfigManager:
_instance = None
_initialized = False
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def __init__(self, filename="app.ini"):
# Guard: don't re-run on subsequent construction calls
if ConfigManager._initialized:
return
print(f"Loading {filename} — this runs ONCE")
self.filename = filename
self.settings = {"debug": True, "version": "2.4"}
ConfigManager._initialized = True
a = ConfigManager("app.ini") # prints "Loading app.ini..."
b = ConfigManager("different.ini") # silent — already initialised
print(a.filename) # app.ini (still the first value)
print(a is b) # True
Alternative Singleton Patterns
The __new__ approach is the most obvious, but Python offers several equivalent
patterns. Each fits a different taste in code style.
Pattern 1 — Decorator
def singleton(cls):
instances = {}
def get_instance(*args, **kwargs):
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
return get_instance
@singleton
class Database:
def __init__(self, host):
self.host = host
db1 = Database("localhost")
db2 = Database("other-host")
print(db1 is db2) # True
print(db1.host) # localhost (the second call was ignored)
Pattern 2 — Module-Level (the Pythonic default)
# In config.py — module itself IS the singleton
settings = {"debug": True, "version": "2.4"}
def get(key):
return settings.get(key)
# Anywhere else:
# from config import settings, get
# Every import gets the same module → same settings dict — natural singleton
In many real Python codebases, the "singleton" is just a module. Python
caches imported modules once, so import config in ten different files gives
every file the same config object. No __new__, no decorator, no
metaclass — just a plain module. Reach for __new__-based singletons only when
you genuinely need class semantics (subclassing, encapsulation of behaviour).
Practical Example — A Callable Counter With State
A class that acts like a function but remembers how many times it's been called. Classic
__call__ use case.
class Counter:
def __init__(self, start=0):
self.count = start
def __call__(self):
self.count += 1
return self.count
def reset(self):
self.count = 0
def __repr__(self):
return f"Counter(count={self.count})"
c = Counter()
print(c()) # 1 ← invoked like a function
print(c()) # 2
print(c()) # 3
# Still an object — you can inspect and reset
print(c) # Counter(count=3)
c.reset()
print(c()) # 1 (started over)
# Multiple independent counters — each carries its own state
hits = Counter()
misses = Counter()
hits(); hits(); hits()
misses()
print(f"hits={hits.count}, misses={misses.count}") # hits=3, misses=1
Practical Example — A Callable Validator
Combine __call__ with __init__-time configuration to build small,
reusable validation objects. Configure once, call anywhere.
class MinLengthValidator:
def __init__(self, min_length):
self.min_length = min_length
def __call__(self, value):
# Uses config from __init__ every time it's called
if len(value) < self.min_length:
raise ValueError(
f"{value!r} must be at least {self.min_length} chars"
)
return value
# Build once, use everywhere
username_valid = MinLengthValidator(3)
password_valid = MinLengthValidator(8)
# Call the instances like functions
print(username_valid("alice")) # 'alice'
print(password_valid("SecureP@ss")) # 'SecureP@ss'
try:
password_valid("abc")
except ValueError as e:
print("Blocked:", e)
# Callables can be passed to higher-order functions
inputs = ["alice", "bob", "chandra"]
valid_names = [n for n in inputs if len(n) >= username_valid.min_length]
print(valid_names)
Common Mistakes (and Fixes)
| Mistake | What Goes Wrong | Fix |
|---|---|---|
Forgetting to return from __new__ |
Person(...) returns None because __new__ returned nothing |
Always end __new__ with return cls._instance (or similar) |
Passing self as first parameter to __new__ |
__new__ takes cls, not self — the object doesn't exist yet |
Use def __new__(cls, ...): — never self |
Setting attributes in __new__ that get clobbered by __init__ |
Whatever you set in __new__ is overwritten by __init__ right after |
Set state in __init__. Use __new__ only for creation and identity |
| Singleton with mutable state, no re-init guard | Second call's __init__ silently overwrites the first call's config |
Add an _initialized flag and short-circuit __init__ if already true |
| Singleton in multi-threaded code without a lock | Two threads may both see _instance is None and create two instances |
Guard __new__ with threading.Lock(), or use module-level singleton (which is thread-safe on import) |
Making __call__ mutate self when callers expect purity |
Callers assume obj(x) is like fn(x) — surprising side effects break trust |
Either document the side effect clearly, or use a separate mutating method |
| Using a singleton for testability-critical state | Tests can't easily reset or replace the singleton — flaky tests | Prefer dependency injection over singletons where possible; reset in test fixtures |
Quick Reference
| Task | Syntax | Notes |
|---|---|---|
| Override object creation | def __new__(cls, *a, **kw): | First arg is cls, must return an instance |
| Delegate default creation | super().__new__(cls) | Standard way to create the bare object |
| Fill in attributes | def __init__(self, ...): | Runs AFTER __new__ |
| Make instance callable | def __call__(self, ...): | Then use obj(args) like a function |
| Basic singleton | Store _instance class var, check in __new__ | Simplest and most common pattern |
| Safe singleton | Add _initialized flag, guard __init__ | Prevents re-init on subsequent calls |
| Singleton via decorator | Wrap class in a factory function | Cleaner syntax, no __new__ override |
| Pythonic singleton | Just use a module | Modules are cached — natural singletons |
| Check if callable | callable(obj) | True if the object defines __call__ |
| Check identity | a is b | True if same object in memory |
Golden Rules
ClassName(...) call runs two methods:
__new__ creates the object, __init__ fills it in. Understand this
order — every singleton, immutable subclass, and metaclass depends on it.
__new__ unless you have a reason. The default
is right 99% of the time. Reach for it only for singletons, immutable subclasses of built-ins,
or object pools.
__new__ takes cls, not self — the
object doesn't exist yet. And it must return an instance. Forgetting the
return produces the classic NoneType has no attribute... bug.
__init__ runs every time you call ClassName(...) — even
when __new__ returned an existing instance. For singletons, guard
__init__ with an _initialized flag so config isn't clobbered.
__call__ makes an instance callable with obj(x).
Use it for configured functions (Validator(min=8), Retry(n=3)) and
stateful accumulators (counters, caches). If the object never carries state, a plain
function is clearer.
_instance), check in __new__, hand back the same
object every time. Test with a is b — identity, not equality.
from config import settings gives you a
natural, thread-safe singleton with zero ceremony.
_instance is None at the same time will both create instances. Wrap
__new__'s check in threading.Lock(), or use module-level state.
__new__.
Do I want the instance to be callable like a function? → __call__.
Do I need exactly one instance across the app? → singleton (or a module).
Match the tool to the shape of the problem.