Why aren't python nested functions called closures?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A nested function and a closure are related concepts, but they are not the same thing. A nested function is any function defined inside another function, while a closure is a nested function that captures variables from the enclosing scope and carries that state with it.
A Nested Function Is About Where It Is Defined
If one function is declared inside another, it is nested. That definition says nothing yet about captured state.
Here inner is nested because it lives inside outer, but it does not retain any variable from the surrounding scope. So it is nested, but not a closure in the strict sense.
A Closure Captures Outer Variables
A closure appears when the inner function references a variable from the enclosing function and keeps access to it after the outer function has already returned.
Output:
The returned function remembers factor even though make_multiplier has finished. That remembered binding is what makes it a closure.
Why the Distinction Matters
The distinction is useful because closures have behavior that ordinary nested functions do not. They can preserve state, act as factories, support decorators, and interact with nonlocal rebinding.
Without nonlocal, rebinding count inside inc would create a new local variable instead of updating the captured one.
Python Stores Captured State Explicitly
You can inspect a closure through the function's __closure__ attribute.
That output shows the captured values stored in closure cells. It is a concrete reminder that Python is preserving lexical state, not just nesting function definitions syntactically.
A Common Closure Gotcha
Closures capture names, not snapshots of loop variables. That is why this example surprises many people:
Output:
A common fix is default-argument binding:
That forces the current loop value to be bound at definition time.
Why Not Call Every Nested Function a Closure?
Because doing so would erase an important semantic difference. A nested function may just be a local helper with no preserved state at all. A closure, by contrast, is specifically about captured environment.
That distinction becomes important as soon as you care about state persistence, rebinding behavior, or memory of outer variables after the outer function has returned.
Common Pitfalls
A common mistake is calling every nested function a closure even when no state capture happens. That hides the meaningful part of what a closure is.
Another issue is forgetting nonlocal when trying to rebind captured variables.
Developers also often assume closures capture the value of a loop variable immediately, when in fact they usually capture the name and resolve it later.
Summary
- A nested function is defined inside another function.
- A closure is a nested function that captures variables from an outer scope.
- Every closure is nested, but not every nested function is a closure.
- Closures preserve state after the outer function returns.
- The distinction matters because captured state changes how the function behaves.
Related reading
- Why await doesn't wait asyncio.create_subprocess_exec
- Why can a function modify some arguments as perceived by the caller, but not others?
- Why can I not import Tensorflow.contrib I get an error of No module named 'tensorflow.python.saved
- Why can tuples contain mutable items?
- Why can't a 'continue' statement be inside a 'finally' block?
- Why can't dataclasses have mutable defaults in their class attributes declaration?
- Why can't I build a list by assigning each element in turn? How can I add append the elements without getting an IndexError?
- Why can't I establish connection to rabbitMQ using python?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.