I recently watched a talk by Dave Cheney about first class functions in Go. Python supports first class functions too, so can we use them in the same ways? Absolutely.

I have been using Python for a while now. It makes for a nice complement to Bash when I need to incorporate somewhat complex logic into scripts. It often makes for very clean and readable code when manipulating simple data.

When learning to use a language, I find it important to see what can be done using only the language’s innate features. For instance, learning how to use — and not overuse — list comprehensions is a crucial requirement to mastering Python. This article explores what can be done with first class functions in Python while keeping the code clean.

 

First, what does “Python supports first class functions” mean? Simply put, functions can be used as if they were values. You can store them in variables, pass them to other functions as arguments, etc.

Dave Cheney used a simple calculator as an example, so let’s start with that. Here’s one way of implementing it:

Familiarity with Python — or virtually any object-oriented language — is all it takes to understand the code above. Here’s how it can be used:

Now that the calculator works, this code can be imported into any script that might need it. But, what if the script needs to use division? The calculator does not implement that yet.

 

That capability could be added easily by writing a new method for the Calculator class. Although, what if someone later needs to compute a logarithm? Or factorials? In an ideal world, this calculator should be able to do any kind of numerical computation without needing to have its code edited.

This is where first class functions come in. Remember the definition from earlier: functions can be stored in variables and passed as arguments to other functions.

Here is some code to show how functions can be stored in variables:

In the code above, rand is actually a variable of the ‘function’ type. Notice that also_rand has the same exact value as rand, name and all.

Functions can also be passed as arguments to other functions, like this:

This will remind many Python developers of functions like map and filter that take functions as arguments, just like call_three_times above. This is called functional programming.

Back to the calculator. Users should be able to pass any function to the calculator and have it run that function. Here is how it could be coded:

The code is much shorter now, which is normal: the calculator can do anything but it does not know how to actually do any of it. Here are some operations for it to run:

All of this works, but it does not make for very flexible code. There is an entire function just to add 2. If one wanted to add 5 next, there would need to be an add_five function. This will get old very quickly.

This is a good use case for lambdas. They allow creating short anonymous functions, i.e. functions that are not explicitly defined with def. They can be used to rewrite the code above like this:

While not having to define functions for every operation the calculator should run is nice, there is such a thing as too many lambdas: the code above is not very easy to read. If someone wanted to know what the code above did, they would spend a lot of time reading those lambdas in order to understand exactly what it is they are doing. This code needs to be made more readable.

What if those lambdas could be replaced by functions with explicit names? Kind of like add_two from earlier only with more re-usability. We could write a function that returns a lambda.

Code readability rarely gets any better than calc.do(add(2)).

In the code above, add and multiply are functions that return a function. The returned function is then called by the calculator. This is what functional programming is all about and makes for very readable code.

Lambdas can only use one expression, which makes them great for simple things like addition but insufficient when it comes to more complex logic. Lambdas are not all we can use, however. Any function can be returned.

It is clear now that the calculator can apply any behavior encoded inside a function. The only prerequisite is to respect a simple contract: the function passed to the calculator’s do method must take one value as an argument and return one value. This means that any function that already matches these criteria can be sent to the calculator, including functions from other libraries. For example:

Support for first class functions is no small feature. While using functions in this way may seem intimidating at first, one gets the hang of it pretty quickly.

If there is only one thing to take away from this article, it is that functions can be used to build other functions. While it might seem complicated, it can actually make code much easier to read and understand. It can also allow writing complex — and even configurable — logic that abides by very simple contracts.

The Go community makes extensive use of all this in their code, and it is serving them well. Maybe some of these patterns can be used in Python code as well.