Skip to content

Latest commit

 

History

History
19 lines (13 loc) · 437 Bytes

memoize-function-with-cache.md

File metadata and controls

19 lines (13 loc) · 437 Bytes

Memoize functions with the cache directive

In python one can memoize a function with a single directive.

from functools import cache

# A Fibonacci function with memoization
@cache
def fibo(n):
    if n <= 2:
        return 1

    return fibo(n-1) + fibo(n-2)

One thing to keep in mind is that the parameters of the targeted function must be hashable (for example one cannot use lists as inputs, but can use tuples).