Caching function results

Peter Otten __peter__ at web.de
Thu Mar 3 15:58:02 EST 2016


Pavel Volkov wrote:

> Suppose, I have some resource-intensive tasks implemented as functions in
> Python.
> Those are called repeatedly in my program.
> It's guranteed that a call with the same arguments always produces the
> same return value.
> I want to cache the arguments and return values and in case of repititive
> call immediately return the result without doing expensive calculations.

> The arguments and return values are almost always:
> * ints
> * floats
> * tuple or lists of ints or floats
> 
> I think Cache object will store data in a dictionary.
> I'll convert lists to tuples before storing them.
> 
> I'd also like to limit the size of the cache (in MB) and get rid of old
> cached data.

https://docs.python.org/dev/library/functools.html#functools.lru_cache

The only thing that is missing is list-to-tuple conversion.




More information about the Python-list mailing list