Programming Terms: Closures - How to Use Them and Why They Are Useful
Based on Corey Schafer's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.
A closure is an inner function paired with the environment that binds its free variables, so it can access captured values after the outer scope ends.
Briefing
Closures let an inner function keep access to variables from the scope where it was created—even after that outer scope has finished running. That “memory” is the practical payoff: you can build functions that carry configuration (like a captured message or HTML tag) and reuse them later without re-supplying the same context.
The core idea is easiest to see by returning an inner function from an outer function. In the Python example, an outer function sets a local variable `message` (first to `
Cornell Notes
Closures are inner functions that retain access to variables from the environment where they were created, even after the outer function finishes. In Python, returning an inner function that references a free variable like `message` produces a callable that still prints the captured value later. Adding parameters to the outer function lets each returned closure “remember” a different captured value (e.g., `
Why does returning an inner function create a closure instead of just a normal function call?
What does “free variable” mean in the closure examples?
How do closures differ when the inner function takes parameters (JavaScript example)?
How can closures be used to generate specialized functions from general ones (Python parameterized closure)?
What practical benefit appears in the final Python logging example?
Review Questions
- In the Python closure example, what exactly is captured when the outer function returns the inner function, and how can you tell?
- In the JavaScript `HTML tag` example, which value is captured by the closure and which value is provided later as a parameter?
- How does using `*args` in the Python `logger` closure affect what kinds of functions you can wrap?
Key Points
- 1
A closure is an inner function paired with the environment that binds its free variables, so it can access captured values after the outer scope ends.
- 2
Returning an inner function (instead of calling it) is the mechanism that turns local variables into retained state.
- 3
In Python, a variable referenced by the inner function but defined in the outer function is a free variable, and closures preserve its binding.
- 4
Closures can be parameterized: passing different values to the outer function produces multiple closures that remember different captured data.
- 5
JavaScript closures often capture configuration from the outer function (like an HTML tag) while the returned function still accepts its own arguments later.
- 6
Closures enable function factories—creating specialized functions (e.g., `add logger` and `sublogger`) from general logic.
- 7
A common practical use is instrumentation: wrapping functions to log which function ran and with what arguments, potentially via files or decorators later.