In this lesson, you’ll learn about the functools
module. This module contains some useful higher order functions like reduce()
and some decorators like cached_property
and lru_cache
.
functools.reduce()
is useful to apply a function over and over on an iterable to “reduce” it to one single value:
>>> from functools import reduce
>>> reduce(lambda x, y: x * y, [1, 2, 3, 4])
24
functools.cached_property
is available in Python 3.8 and above and allows you to cache class properties. Once a property is evaluated, it won’t be evaluated again.
functools.lru_cache
allows you to cache recursive function calls in a least recently used cache. This can optimize functions with multiple recursive calls like the Fibonnacci sequence.
You can check out the Python documentation on the functools
module.
James Uejio RP Team on April 27, 2020
Here is the Python documentation on the
itertools
module: Python itertools module