How to memoize functions?

Jerome Alet alet at unice.fr
Thu Jun 26 17:08:19 EDT 2003


Le Thu, 26 Jun 2003 16:29:07 -0400, Chris Reedy a écrit :

> The obvious way to memoize a function would be to keep a dictionary with
> keys being tuples (or maybe dictio naries) of previous argument lists
> and values being the results of the previous computations.
> 
> Unfortunately, this will really mess up garbage collection, since
> objects that are arguments could never be collected.

first I must say that my answer is probably completely stupid.

however why would you want to use the original arguments tuple as a key ?

depending on the computational intensity of your original function, why
not compute some sort of hash (like an hex md5sum) on all the arguments
(converted to strings and concatenated) ?

this way your original arguments would still be garbage collectable (well,
at least I suppose), since only the hash would be used for the key.

again, it may be stupid, I don't know python internal enough

hth

Jerome Alet




More information about the Python-list mailing list