CKTIS / Python
Module functools
Transform callables with tools for partial application, wrapping and memoization.
Version: Python 3.14; repository image, production runtime not attested
Syntax
@functools.lru_cache(maxsize=2)Transform callables with tools for partial application, wrapping and memoization.
functools.partial binds selected arguments and calls the original function with the combined arguments. An LRU cache reuses a previous result for a matching call and evicts the least recently used entry at capacity.
Its cache keys require hashable arguments.
Examples
import functools
@functools.lru_cache(maxsize=2)
def price(units, rate):
return units * rate
double = functools.partial(price, rate=2)
print([double(n) for n in (1, 2, 1, 3, 2)])
print(price.cache_info().hits, price.cache_info().misses)
try:
double([])
except TypeError:
print("unhashable")
Result
[2, 4, 2, 6, 4] 1 4 unhashable
Its cache keys require hashable arguments.