In this article I will introduce you to closures in Python 3. Actually, a closure is a function in Python which allows you to do things that aren’t possible in another language (like C/C++ or Java) with functions. In Python everything is treated like a first-class citizen, so they can be passed around just as normal variables. So are functions too. You probably have seen code like this already:
Because we can pass around functions as arguments we can return functions too from function calls. Let's consider the following example:
And these functions are closures.
What makes closures special?
If you think that closures are ordinary functions you might have missed the key difference from the previous example.
Call the contains_15 with some lists / iterables to test the functionality:
Another interesting fact is, that the closure remains existing even if the original creator function (in the example case it is contains_factory) is deleted:
After this introduction on closures you should know how to create closures in Python. But for the sake of brevity let's sum things up:
Pretty simple, isn't it? The second point is simple in reality, optional -- but if you do not reference a variable from the enclosing function there is not much sense to create a nested function and return it -- you simply define a function. You could do this in the normal scope too.
Let's create another interesting closure: a counter. The idea behind the counter is that in some cases you just want to count interactions. In those cases you define a global variable (most of the time called counter) and increment it at the right place when an interaction occurs. We can replace this global variable and the incrementation by defining a closure and call this closure every time we want to count something:
As you can see, the incrementer increments silently the value of count inside the closure. getter returns the actual value of count without incrementing it -- and this makes the feature usable. Resetting the counter is done by simply re-assigning the closures.
What are closures good for?
Even if closures seem pretty interesting (a function returning another function which knows its creation context!) there is another question: where can we utilize closures to make the best of them?
Here are a few uses for closures:
Late binding of closures
A source of problems is the way Python binds the variables in closures. For example you write:
In reality you get:
What happens? You create 10 functions which add 1 to the number 9. This is because of the late-binding of closures: the variables used inside the closures are looked up at the time the inner function is called. Whenever the adder function is called the inner loop over the range(10) is finished and the variable i has the value 9 -- so you end up with 10 for each 10 functions as result.
To solve this problem you could add an additional parameter to your closures which will maintain the right state:
We have seen that closures are functions which remember the context (lexical scope) in which they were created and they can use it even if the program flow is no longer in the enclosing scope. To create a closure you need to write a function which returns another function. The returned function is the closure itself.
Closure variables are late binding, so take care when you use them: you may end up with unwanted results!
* Python Closures and the Python 2.7 nonlocal Solution