lru_cache
One subtle but very real bug when using functools.lru_cache with mutable objects is cache poisoning caused by mutability. The cache stores and returns the exact same object references on every call. If a cached function returns the object and a caller later modifies it in place, the cached value itself becomes corrupted. For example:
1
2
3
4
5
6
@lru_cache(max_depth=1)
def icosahedron_2_sphere(level:int):
return Obj
V, F = icosahedron_2_sphere(2)
V /= 2 # in-place modification — corrupts the cached array
V /= 2 mutates the array in place, whereas V = V/2 creates a new array.