Search code examples
pythonmemoization

How to apply functools.lru_cache to function with mutable parameters?


I have a function with one of the parameters as numpy.ndarray. It's mutable so it can not be cached by lru_cache.

Is there any existing solution for this?


Solution

  • Possibly the simplest way of doing so is to memoize a version taking only immutable objects.

    Say your function takes an np.array, and let's assume it's a 1d array. Fortunately, it is easily translated to a tuple:

    import numpy as np
    
    a = np.array([1, 2, 3, 4])
    >> tuple(a)
    (1, 2, 3, 4)
    

    and vice versa:

    >> np.array(tuple(a))
    array([1, 2, 3, 4])
    

    So you get something like

    # Function called by the rest of your program
    array_foo(a) # `a` is an `np.array`
        ...
        return tuple_foo(tuple(a))
    

    then memoize instead this function:

    # Internal implementation
    @functools.lru_cache
    tuple_foo(t) # `t` is a tuple
        ...
        a = np.array(t)