Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python @memoize vs functools.lru_cache

Tags:

python

I'm not sure what the advantages and disadvantages of these 2 are. Provided @lru_cache from functools is a standard library tool and seems to have more control over @memoize, why does @memoize even exist?

Edit: I am referring to memoize from here -> https://wiki.python.org/moin/PythonDecoratorLibrary#Memoize

like image 410
s5s Avatar asked Sep 03 '17 10:09

s5s


1 Answers

functools.lru_cache was added in python 3.2. For those using python 2, and for libraries written to work with it (e.g. all major libs) it cannot be used.

The workaround is to use the @memoize recipe from the decorator library.

If you only use python 3, there is basically no reason not to use lru_cache.

like image 112
Jonas Adler Avatar answered Sep 19 '22 12:09

Jonas Adler