The LEGB rule — where Python looks for a name
When you reference a name like x, Python looks in (in order): Local scope, Enclosing function scopes, the Global (module) scope, and finally Built-ins (print, len, etc.). The first place it finds the name wins. This is why a name defined in a function shadows a same-named name at module level — Python finds the local one first.
Closures — functions that remember their birthplace
An inner function can refer to names from the enclosing function. When you return that inner function, it carries those bindings with it — that's a closure. The inner function holds a reference to the enclosing function's variables, even after the enclosing function has returned. This is how decorators work, how factory functions work, and how callbacks remember context.
Reading vs. writing — the asymmetry
Inner functions can read enclosing variables freely. Writing to them requires nonlocal. Writing to module-level variables requires global. Without these declarations, an assignment creates a new local binding that shadows the outer name. This single rule trips up almost every beginner who tries to mutate a counter from inside a closure.
fns = [lambda: i for i in range(5)]. Every lambda refers to the same i, which is 4 at the end. Capturing the current value requires lambda i=i: i (default arg captures at definition time). This bites everyone once.
When to reach for closures vs. classes
If you need a function that carries some state, a closure is light and idiomatic. If you need multiple methods sharing state, a class is clearer. The blurry middle is one of the perennial Python design questions — and there's no rule that fits every case.
A closure is not automatically a tiny class. When one operation owns the state and nothing else needs to inspect, reset, or serialize it, the closure is the clearer boundary. Once several behaviors share a persistent identity, promoting that state to an object becomes honest. In Dad's terms, the closure remains the right shape while the one strategy being encapsulated matters more than object identity.