Message274151
This behaviour is expected. The factorial function calls itself, it doesn't call "f", but it is "f" which has the cache. So the call to f() goes through the cache, misses, and then calls factorial(), which has no cache.
In effect, what you have written is something like:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
def f(n):
if n in f.cache:
return f.cache[n]
else:
x = f.cache[n] = factorial(n)
return x
f.cache = lru_cache() |
|
Date |
User |
Action |
Args |
2016-09-01 16:35:53 | steven.daprano | set | recipients:
+ steven.daprano, rhettinger, ncoghlan, Федор Лянгузов |
2016-09-01 16:35:53 | steven.daprano | set | messageid: <1472747753.43.0.0996727547652.issue27933@psf.upfronthosting.co.za> |
2016-09-01 16:35:53 | steven.daprano | link | issue27933 messages |
2016-09-01 16:35:53 | steven.daprano | create | |
|