Memoize a function so that it isn't reset when I rerun the file in Python
I often do interactive work in Python that involves some expensive operations that I don't want to repeat often. I'm generally running whatever Python file I'm working on frequently. If I write: import functools32 @functools32.lru_cache() def square(x): print "Squaring", x return x*x I get this behavior: >>> square(10) Squaring 10 100 >>> square(10) 100 >>> runfile(...) >>> square(10) Squaring 10 100 That is, rerunning the file clears the cache. This works: try: safe_square except NameError: @functools32.lru_cache() def safe_square(x): print "Squaring", x return x*x but when the function is