9.8 Creating a partial function
When we look at functions such as reduce(), sorted(), min(), and max(), we see that we’ll often have some argument values that change very rarely, if at all. In a particular context, they’re essentially fixed. For example, we might find a need to write something like this in several places:
reduce(operator.mul, ..., 1)
Of the three argument values for reduce(), only one – the iterable to process – actually changes. The operator and the initial value argument values are essentially fixed at operator.mul and 1.
Clearly, we can define a whole new function for this:
from collections.abc import Iterable
from functools import reduce
import operator
def prod(iterable: Iterable[float]) -> float:
return reduce(operator.mul, iterable, 1)
Python has a few ways to simplify this pattern so we don’t have to repeat the...