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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With