On this page
  1. functools.cache
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

functools.cache

Up: functools | Next: functools.cached_property
Rel: functools.cached_property, functools.cmp_to_key, functools.lru_cache, functools.partial
Ref: docs.python.org — functools.cache
Tags: #public #python3 #stdlib #functools


Import

import functools
from functools import cache

Signature & Overview

functools.cache(user_function, /)

Simple lightweight unbounded cache. Sometimes called "memoize".

Usage

from functools import cache

@cache
def fib(n: int) -> int:
    return n if n < 2 else fib(n - 1) + fib(n - 2)

assert fib(30) == 832040
assert fib.cache_info().hits > 0

Gotchas

REPL Inspect

from functools import cache
help(cache)

Linked from

Related notes