A showcase of Python’s functools module
Python has three built-in modules that greatly help when writing code in a functional dialect: functools
, operator
, and itertools
. In this recipe, we are going to briefly discuss the functools
module. For example, the fundamental reduce
function (where part of the name of MapReduce comes from) is only available if you import functools
.
While a detailed exploration of these modules would be too long for a single recipe, we are going to showcase some functionality by improving some of the code of the previous recipes with the functionality from functools
and showcase some illustrative examples of the utility of the module.
Getting ready
Our code is available in Chapter12/Builtin.py
. We will make references to previous recipes.
How to do it...
Let’s look at several illustrative examples:
- Remember that our recursive implementation of a factorial function in the previous recipe was not very efficient? Let’...