Get AI summaries of any video or article — Sign up free
Programming Terms: Closures - How to Use Them and Why They Are Useful thumbnail

Programming Terms: Closures - How to Use Them and Why They Are Useful

Corey Schafer·
3 min read

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.

TL;DR

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?

In the Python pattern, the outer function creates a local variable (like `message`) and defines an inner function that references that variable. When the outer function returns the inner function (without calling it), the inner function still has access to the local variable it referenced. That retained link to the outer environment is what makes it a closure: the inner function can be invoked later and still “remembers” the captured `message`.

What does “free variable” mean in the closure examples?

A free variable is a name used inside the inner function that is not defined within that inner function. In the Python example, `message` is defined in the outer function, not inside the inner function. Because the inner function reads `message` from the outer scope, `message` is a free variable for the inner function—and closures preserve the binding of that free variable.

How do closures differ when the inner function takes parameters (JavaScript example)?

In the JavaScript example, the outer function `HTML tag(tag)` captures `tag` and returns an inner function `wrap text(message)`. Here, the captured variable is `tag`, while `message` becomes a parameter supplied when the returned function is called. So the closure stores the environment for `tag`, but the returned function still accepts its own arguments later.

How can closures be used to generate specialized functions from general ones (Python parameterized closure)?

When the Python outer function accepts a parameter `msg` and assigns it to `message`, each call to the outer function returns a new inner function bound to that specific `message`. Creating `hi function` and `hello function` from the same outer logic demonstrates that each closure remembers its own captured value, so calling them later prints different messages without passing arguments again.

What practical benefit appears in the final Python logging example?

The outer function `logger` takes a function as input and returns an inner `log funk` that logs which function is being run and with what arguments (using `*args` to accept any number of arguments). Two closures—`add logger` and `sublogger`—wrap `add` and `subtract` respectively. Running them produces correct results while also generating a log file (`example.log`) that records the function name and arguments for each call.

Review Questions

  1. In the Python closure example, what exactly is captured when the outer function returns the inner function, and how can you tell?
  2. In the JavaScript `HTML tag` example, which value is captured by the closure and which value is provided later as a parameter?
  3. How does using `*args` in the Python `logger` closure affect what kinds of functions you can wrap?

Key Points

  1. 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. 2

    Returning an inner function (instead of calling it) is the mechanism that turns local variables into retained state.

  3. 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. 4

    Closures can be parameterized: passing different values to the outer function produces multiple closures that remember different captured data.

  5. 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. 6

    Closures enable function factories—creating specialized functions (e.g., `add logger` and `sublogger`) from general logic.

  7. 7

    A common practical use is instrumentation: wrapping functions to log which function ran and with what arguments, potentially via files or decorators later.

Highlights

Closures make it possible for an inner function to “remember” a local variable like `message` even after the outer function has finished.
Capturing `tag` in JavaScript lets one returned function format messages for `<h1>` while another formats for `<p>`—without duplicating logic.
Using `logger` plus `*args` creates reusable wrappers that can log any number of arguments while still executing the original function.
The final Python example demonstrates a real payoff: closures can generate a log file (`example.log`) that records function calls and arguments.

Topics

Mentioned