All the solutions I managed to find are only on lru_cache. But in my case dir(functools) shows that that very lru_cache does reside in functools while cache does not! How can I solve this?
All the solutions I managed to find are only on lru_cache. But in my case dir(functools) shows that that very lru_cache does reside in functools while cache does not! How can I solve this?
The documentation for functools.cache states that it's only available from Python 3.9 onwards. If you're using an earlier version then the documentation also states that it's the same as using lru_cache(maxsize=None), so that's probably your best option.
from functools import lru_cache
@lru_cache(maxsize=128)
def fib(n):
if n <= 1:
return n
return fib(n-1) + fib(n-2)
def main():
for i in range(1000):
print(i, fib(i))
print('Done')
if __name__ == '__main__':
main()