[issue27933] functools.lru_cache seems to not work when renaming decorated functions

Steven D'Aprano report at bugs.python.org
Thu Sep 1 12:35:53 EDT 2016


Steven D'Aprano added the comment:

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()

----------
nosy: +steven.daprano

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue27933>
_______________________________________


More information about the Python-bugs-list mailing list