Functional Functions

functools.cache()

Wrap a function with an unbounded memoization cache keyed by hashable arguments. Cache hits reuse results; concurrent first calls may execute more than once.

Formal Syntax & Structure

python syntaxpython
functools.cache(user_function, /)

Parameters & Attributes

NameTypeDescription
user_functioncallableFunction to wrap.

Return Value

callable

The callable result described above.

Working Example

Example Snippetpython
import functoolsf=functools.cache(lambda x:x*2)print((f(4),f(4),f.cache_info().hits))
Python Reference