Hashing a python function to regenerate output when the function is modified

Posted by Seth Johnson on Stack Overflow See other posts from Stack Overflow or by Seth Johnson
Published on 2010-04-26T20:41:54Z Indexed on 2010/04/26 21:43 UTC
Read the original article Hit count: 197

Filed under:
|
|

I have a python function that has a deterministic result. It takes a long time to run and generates a large output:

def time_consuming_function():
    # lots_of_computing_time to come up with the_result
    return the_result

I modify time_consuming_function from time to time, but I would like to avoid having it run again while it's unchanged. [time_consuming_function only depends on functions that are immutable for the purposes considered here; i.e. it might have functions from Python libraries but not from other pieces of my code that I'd change.] The solution that suggests itself to me is to cache the output and also cache some "hash" of the function. If the hash changes, the function will have been modified, and we have to re-generate the output.

Is this possible or ridiculous?


Updated: based on the answers, it looks like what I want to do is to "memoize" time_consuming_function, except instead of (or in addition to) arguments passed into an invariant function, I want to account for a function that itself will change.

© Stack Overflow or respective owner

Related posts about python

Related posts about hash