Marten Lienen <[email protected]> added the comment:
The lru_cache can trigger infinite recursion if it is used to cache a hash
computation because the cache look-up itself requires the hash.
from functools import lru_cache
class CachedHash:
@lru_cache
def __hash__(self):
# Expensive calculation because we are caching a big matrix for example.
return 0
hashed = CachedHash()
hash(hashed) # => RecursionError: maximum recursion depth exceeded while
calling a Python object
Even though this looks contrived, it is actually my use case. cached_property
is not directly applicable because __hash__ needs to be a method and I wanted
to avoid caching as an instance attribute because my class is a frozen
dataclass. The dataclass thing also makes close() awkward because then I would
have an outwardly resource-ful dataclass which is against the spirit of a
dataclass.
I will think about putting this on pypi instead.
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue45588>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com