[issue45588] cached_method similar to cached_property to cache with classes

Raymond Hettinger report at bugs.python.org
Mon Oct 25 20:36:34 EDT 2021


Raymond Hettinger <raymond.hettinger at gmail.com> 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.

Yes, I see the problem.  Am not sure whether I should add a note to the docs for this.


> 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.

If you think of a dataclass as just a data holder like a mutable named tuple, I can see where the notion of the "spirit of dataclass" comes from.

However, if you think of it as a code generator for handling the boilerplate code in a more complex class, the only "spirit" is to do anything that any other class does.

For objects that hold resources, an explicit close() method is common and well supported (i.e. contextlib.closing and generator.close()).  It is a perfectly reasonable thing to do.

That said, it's a matter of taste.  Just do what works best for you.


> I will think about putting this on pypi instead.

If you do post it, let me know.  I'll add a link to it from the FAQ entry.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45588>
_______________________________________


More information about the Python-bugs-list mailing list