Skip to content

.tip

· one of the small things

functools.cache on pure functions that are called repeatedly

Think of a settings loader, a compiled regex, or a lookup that reads from disk. If the inputs are hashable and the output never changes, you can cache it in one line.

@functools.cache
def load_schema(name: str) -> dict: ...

Use lru_cache(maxsize=…) when the input space is large. Neither one is right for anything with side effects, or for values that change while the process is running.

pythonperformance