PyDocs

Nav

Functional Programming Modules

cache

functools

decorator · Added in 3.9 (source)
  • @cache(user_function)
Simple lightweight unbounded cache.  Sometimes called "memoize".
1 Example
@cache
def factorial(n):
    return n * factorial(n-1) if n else 1

>>> factorial(10)   # no previously cached result, makes 11 recursive calls
3628800
>>> factorial(5)    # no new calls, just returns the cached result
120
>>> factorial(12)   # two new recursive calls, factorial(10) is cached
479001600
from the Python docs
See Also
No see-alsos for functools/cache
0 Notes
No notes for cache