Closures are a powerful and flexible way to create new functions out of existing functions. You can think of them as being function factories, that can create new functions according to a template and one or more parameters.

In this article we will look at how to use a closure to compose two functions, as a simple illustration of why and how we use closures.

Image for post
Image for post

This article first appeared on The topic is covered in more detail in my e-book Functional Programming in Python.

Composing two functions simply means applying one function to the result of another. For example, consider these two standard Python functions:

str(x)converts any value x to a string. len(x)returns the length of any sequence x. The composition of these two functions is

An inner function is a function that is defined within the scope of another function, like this:

In this case our outer function add_3 adds 3 to the value of xand returns the result.

The add_3function contains:

  • A local variable nthat is set to 3.
  • An inner (or ‘local’) function fthat adds nto the supplied parameter x

It uses these to calculate the result of v+ 3. Of course, you could write a much simpler function to do the same job, we are simply using it to illustrate the idea of inner functions.

The local variable ncannot be accessed from outside the print_add_3function. obviously, since it is a local variable. In a similar way, the inner function fcan’t be accessed from outside the print_add_3function either, it is also local. This makes sense, of course, the reason we use local variables and inner functions is to hide the inner workings of our function from the outside world. This is called encapsulation.

It is worth noting that the function fis able to access the variable n, because they are both within the scope of the outer function print_add_3.

As an aside, though quite an important one, you may have heard it said that in Python everything is an object. Well that isn’t just true of numbers, strings, lists etc, it is true of functions too! So:

calls the function min, and prints the result. But without the brackets:

will display <built-in function min>, the function object for min. You can even assign this function to a variable, and then call it using bracket notation:

This will print 2, because a references the min function object, and a(3, 2) calls it. We call this aliasing: a is an alias of min.

Now we will rearrange things a little:

The first thing to notice here is that get_add_3 returns f. It doesn’t call f, it returns the function object f.

So our new function get_add_3 doesn’t add 3 to a number, instead it returns a function object f that adds 3 to a number. This means that we need two steps to add 3 to a number:

  • We call get_add_3, which returns a function object, which we store as add_3.
  • We then call add_3(6), which uses the function object to do the calculation.

Now you might have noticed something slightly odd here. add_3 is an alias of f, and f uses the value of n, which is a local variable of get_add_3. But add_3 is being called outside the context of get_add_3, because we have already returned from the call.

Now add_3 obviously knows the value of n because it knows to add 3 to its arguments. So what is going on?

Well, when the function f is returned, Python attaches extra information to the function object, storing the values of any free variables within the outer function.

In fact, this is a closure, it is just that at the moment it isn’t a particularly useful closure.

The final step is to make this into something more useful:

This time, when we call add_n, we actually pass in our required n value. The function returns a closure of d that includes the requested value of n.

So add_n(4) creates a function that adds 4 to a value. We can store this in a variable add_4 and then call add_4() as a function — because that is exactly what it is. Likewise, we can create an add_7 function if we need one, or any n we require.

Closures are basically function factories, and very useful ones at that.

You might wonder why we would go to all the trouble of creating a closure to add 4 to x. Why not just do:

Well, in functional programming you generally want to deal with functions rather than expressions. Of course we could do this:

But that requires us to hand write a function, which could easily introduce bugs. Now assuming we have an add_n closure already defined, and we trust it because it is part of a well tested library, we can just do this:

This is far more declarative — we are saying what we want rather than describing how to do it. It is both more readable and less error prone.

To summarise what we have done so far, a closure requires three things:

  • An outer function that contains an inner function.
  • The outer function has parameters and/or local variables.
  • The outer function returns the inner function as a function object.

Suppose we wanted to take an object, convert it to a string, and find how long that string is. We could do it like this:

That is all very well, but what if we wanted to apply this operation to a list of values using the map function. map requires a function object and a list or other iterable. So we would have to create a special lenstr function:

This would work but is it a very procedural approach. Again, we are defining a special function, which is error prone.

Suppose instead we had a compose closure, that takes two functions, f and g and returns a new function that finds f(g(x)):

compose is quite a general purpose function, available in several popular Python functional programming libraries. Now we can do this:

This creates a lenstr` function without any procedural coding. Or we can just use compose` directly without storing the function:

Again, this is far more declarative. Rather than hand coding a special lenstr function, we are simply declaring a function that is a composition of len and str. It is clearer and more reliable.

Written by

I am a software developer with over 30 years experience in Java, Python and C++. I write for

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store