Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: strategies for persistently memoizing functions with function arguments?

I have written a little class to persistently memoize some expensive functions that do various statistical analyses of random networks.

These are all pure functions; all the data is immutable. However, some of the functions take functions as arguments.

Making keys based on these arguments is a small problem, since in Python function object equality is equivalent to function object identity, which does not persist between sessions, even if the function implementation does not change.

I am hacking around this for the time being by using the function name as a string, but this raises its own swarm of issues when one starts thinking about changing the implementation of the function or anonymous functions and so on. But I am probably not the first to worry about such things.

Does anybody have any strategies for persistently memoizing functions with function arguments in Python?

like image 505
Gabriel Mitchell Avatar asked Feb 19 '12 22:02

Gabriel Mitchell


1 Answers

One option would be to use marshal.dumps(function.func_code)

It'll produce a string representation for the code of the function. That should handle changing implementations and anonymous functions.

like image 196
Winston Ewert Avatar answered Sep 24 '22 06:09

Winston Ewert