functools Module


Functools module provides tools for working with functions and other callable objects, to adapt or extend them from new purposes without completely rewriting them. In other words, it gives me the tools for manipulating functions such as cached_property decorators. This module is available in python version 2.5 and later. Now, let’s look at some tools that it provides.


cached_property

Useful for expensive computed properties of instances that are otherwise effectively immutable. Available in Python 3.8 and above and allows to cache class properties. For versions before 3.8, you can use cache and property decorators. Example:

from functools import cached_property

class Data:
    def __init__(self, n):
        self.n = n

    @cached_property
    def f(self):
        total = 0
        for i in range(self.n):
            for j in range(self.n):
                total += i + j
        return total

When we instantiate an object and call it, it returns instantly.

>>> data = Data(500)
>>> data.f
124750000

The cached value can be cleared by deleting the attribute.


lru_cache

Another useful functool is lru_cache which allows you to cache recursive function calls in a least recently used cache such as functions with multiple recursive calls like the Fibonnacci sequence. In code:

from functools import lru_cache

@lru_cache
def fib(n):
    if n <= 1:
        return n
    return fib(n - 1) + fib(n - 2)


reduce()

functools.reduce() is useful to apply a function over and over on an iterable to reduce it to one single value. For instance:

>>> from functools import reduce
>>> reduce(lambda x, y: x + y, [2, 4, 6, 8]) # (((2 + 4) + 6) + 8)
20


See also: