`__init__`
__init__ is the instance initialiser. After __new__ creates the object, __init__(self, ...) fills in its attributes. It is the method you write for almost every class that holds state.
Detailed Explanation
People call __init__ the constructor. The real constructor is __new__. __init__ receives the already-created instance as self and should return None. Its job is to put the object into a usable state: store arguments, compute derived fields, open nothing that you cannot also close (prefer a context manager for resources).
You do not call __init__ yourself in normal code. Order("A-1", 2) does it. A subclass that overrides __init__ and still needs the parent's setup calls super().__init__(...). Forgetting super() is the usual inheritance bug: the parent attributes never appear, and a later method fails with AttributeError.
Python does not overload functions. One class has one __init__. Alternative constructors are @classmethods that parse another shape and then call cls(...). datetime.fromisoformat is that pattern. Do not try to detect argument types inside __init__ unless you are wrapping a legacy API; it hides the contract.
Default arguments in __init__ have the same trap as everywhere else. def __init__(self, items=[]): shares one list across all instances. Use None and assign self.items = [] if items is None else items. If you assign self.items = items, callers can still mutate the list they passed in; copy when the object should own the data.
__init__ can raise. If it does, the instance is abandoned and __del__ may still run. Do not start threads or register callbacks in __init__ that keep a reference to self unless you have a clear teardown path. A half-initialised object that is also registered globally is a leak.
Dataclasses write __init__ for you from field declarations. You can still add __post_init__ for derived values. If you write __init__ by hand on a dataclass you take the whole job back.
Code Examples
class Order:
def __init__(self, sku: str, qty: int = 1):
if qty < 1:
raise ValueError("qty must be >= 1")
self.sku = sku
self.qty = qty
def __repr__(self) -> str:
return f"Order({self.sku!r}, {self.qty})"
print(Order("A-1", 2)) # Order('A-1', 2)class PriorityOrder(Order):
def __init__(self, sku: str, qty: int, priority: int):
super().__init__(sku, qty) # initialise the parent first
self.priority = priority
print(PriorityOrder("A-1", 1, 10).sku)class Cart:
def __init__(self, items: list[str] | None = None):
self.items = list(items or []) # own a copy, never a shared default
a, b = Cart(), Cart()
a.items.append("A-1")
print(b.items) # []Common Gotchas
Shared mutable defaults
`items=[]` in `__init__` is one list for every instance that omitted the argument.
Forgetting `super().__init__`
subclass methods then see a missing parent attribute. Call `super()` unless you are replacing the parent state on purpose.
`__init__` must return `None`
returning `self` raises `TypeError`.
`__init__` is skipped if `__new__` returns an object of another type
that is how some immutables and singletons work. Do not be surprised when your `__init__` does not run.