Preamble
About a year ago, a friend and I were discussing closures in programming languages, and how they were treated so specially in many technical circles. To us, the concept seemed weird. Python’s closures were… intuitive, weren’t they? Both of us were not exactly from a standard programming background, we’d both studied Mechanical Engineering, not Computer Science, but we were (and still are) super passionate about tech.
Which is why we were confused. Whatever it is that the world calls closures seems pretty obvious. At least to us they were.
And well, we weren’t wrong. But we were mildly surprised at how Python deals with it.
Intended Audience
This is an intermediate level article, and you might be confused reading it if you don’t understand the following:
- Writing Python scripts.
- Using the REPL Interpreter
- Writing functions
- Writing classes (knowledge of inheritance is not required)
- Using the
id
,help
anddir
functions. - Using the
dis
module to disassemble Python code.
I have linked to the best articles (IMO) on these topics above, so if you like, you can go ahead and read those first.
Also, most articles deal with an article with respect to nested functions. While I do deal with nested functions, I do not start there. I start earlier, with a simple variable, and build up to nested… let’s say objects.
Introduction
Let’s take a look.
|
|
The id
function in Python is a built-in that returns the identity of the object that represents the variable. I use this function to sniff around my code to see if I am passing around copies of a variable or if I am actually passing the variable (sic object) itself.
In this case, it is easy to test this theory.
This snippet prints out the id
value of these variables, and while of course this number will vary, and it could be the same, but for objects with non-overlapping lifetime. In other words, during one specific scope, these numbers are guaranteed to be representative of a specific object.
Scopes
The scope that I just brought up is, in very simple terms, like a space for your code to run in. In python, variables are shared from an outer scope to an inner scope.
|
|
A higher scope is indicative of a scope that contains another scope. If that is too wordy for your taste:
|
|
In this example, scope 0 contains scope 1. Scope 1 contains scope 2. However, everything in scopes 0 and 1 are accessible in scope 2, and everything in scope 0 is accessible to scope 1.
Scope 0 is the outermost scope. Scope 1 is inside scope 0. Scope 1 is outside of scope 2. Scope 2 is inside scope 1.
This will become clearer in time.
Sniffing around returned values
Now, let’s look back at our closures. We saw that the id
value of the integer that was returned is the same. which means, technically, that the same object was returned into the outer scope from the inner scope.
Let’s expand this example.
|
|
When I run the above snippet, here’s what I get:
If you are following along, this means that the application passed around a variable x
, created inside func
, outside of its scope, and then into the scope of another function that returned it unmodified, and the id
was never changed. This means that all through your process, you passed around a single object. You did not change it.
Now, you might wonder if this would work.
|
|
When I run this, here’s what I get:
This doesn’t work. Why? Let’s take it apart using another tool in the python standard library, the dis
module.
Disassembling our snippets
The dis
module allows us to see the bytecode that Python generates from our source code. This can help us understand what’s happening under the hood:
|
|
When you examine the bytecode, you’ll see that the assignment inp = inp**2
creates a new object (the result of the power operation) and assigns it to the local variable inp
. The original object is not modified - instead, a new integer object is created.
inp**2
, Python creates a new integer object rather than modifying the existing one. This is why the id
values are different.
Let’s return a dictionary instead!
Let’s see what happens with mutable objects:
|
|
With mutable objects like dictionaries, the same object reference is passed around, and modifications affect the original object.
Nested Functions - Finally
Now let’s get to the heart of closures with nested functions:
|
|
Here’s where it gets interesting. Even after outer_function
has finished executing, the inner_function
still remembers the value of outer_variable
. This is a closure - the inner function “closes over” the variables from its enclosing scope.
Let’s examine what makes this possible:
|
|
__closure__
and what it means
The __closure__
attribute reveals the secret behind closures. It contains a tuple of cell objects that hold the values from the enclosing scope:
|
|
Practical Example: Building a Counter
Here’s a practical example that demonstrates the power of closures:
|
|
Each counter maintains its own state through closures, providing a clean way to create stateful functions without classes.
Decorators and Closures
Decorators are perhaps the most common use of closures in Python:
|
|
The decorator function returns a closure that wraps the original function, maintaining access to the original function reference.
Common Pitfalls: Late Binding
Be careful with closures in loops:
|
|
A Personal Story
The conversation at the beginning of this article came about because I had just come out of an interview where the interviewer asked me to explain Python closures. I had faltered, not because I didn’t know, but because, oddly enough, I thought there was nothing special about how you can essentially play ping pong with objects in Python. Most languages do this. Python does this only because C does it. You can return pointers, can’t you?
All in all, a confusing interview led me to understand something in depth, and helped me learn.
Conclusion
Closures in Python are indeed intuitive once you understand the underlying concepts of scope and variable lifetime. They provide a powerful mechanism for:
- Creating stateful functions without classes
- Implementing decorators
- Building factory functions
- Maintaining encapsulation
The key insight is that inner functions can capture and remember variables from their enclosing scope, even after the outer function has finished executing. This creates a “closed over” environment - hence the term “closure.”
Understanding closures deeply will make you a better Python programmer and help you write more elegant, functional code.