2 ms·
It's a feature! Use this memoization pattern and you'll never forget it def foo(x, cache={}): if x in cache: return cache[x] val = cache[x] =
by klyrs 6y ago
It's a feature! Use this memoization pattern and you'll never forget it
def foo(x, cache={}):
if x in cache:
return cache[x]
val = cache[x] = x*x+1
return val
But, be warned, there's no mechanism for cache eviction; use @lru_cache if you don't know ahead of time that x will take a reasonably small number of values