[issue45588] cached_method similar to cached_property to cache with classes

2021-10-31 Thread Marten Lienen
Marten Lienen added the comment: As suggested, I have extracted the code and tests into a package on PyPI: https://pypi.org/project/cached_method/ With this I will close this issue. "third party" sounds about right as the Resolution setting. @eric.araujo This feature is about caching the

[issue45588] cached_method similar to cached_property to cache with classes

2021-10-30 Thread Éric Araujo
Éric Araujo added the comment: The feature proposed here is not clear to me. Is it about caching the method object on the instance to optimize the creation of the bound method, as the name suggests? Or is it about caching the result of calling the method, which is consistent with the

[issue45588] cached_method similar to cached_property to cache with classes

2021-10-25 Thread Raymond Hettinger
Raymond Hettinger 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

[issue45588] cached_method similar to cached_property to cache with classes

2021-10-25 Thread Marten Lienen
Marten Lienen 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

[issue45588] cached_method similar to cached_property to cache with classes

2021-10-24 Thread Raymond Hettinger
Raymond Hettinger added the comment: > In my use case, the objects hold references to large blocks > of GPU memory that should be freed as soon as possible. That makes sense. I can see why you reached for weak references. Would it be a workable alternative to have an explicit close()

[issue45588] cached_method similar to cached_property to cache with classes

2021-10-24 Thread Marten Lienen
Marten Lienen added the comment: Central control over the cache and its parameters is definitely a big plus. In my use case, the objects hold references to large blocks of GPU memory that should be freed as soon as possible. Additionally, I use cached_method to cache expensive hash

[issue45588] cached_method similar to cached_property to cache with classes

2021-10-24 Thread Raymond Hettinger
Raymond Hettinger added the comment: For comparison, here is a recipe that I was originally going to include in the FAQ entry but later decided against it. It only had an advantage over @lru_cache with instances so large that we can't wait for them to age out of the cache. It shouldn't be

[issue45588] cached_method similar to cached_property to cache with classes

2021-10-23 Thread Raymond Hettinger
Raymond Hettinger added the comment: See also: https://docs.python.org/3/faq/programming.html#how-do-i-cache-method-calls -- ___ Python tracker ___

[issue45588] cached_method similar to cached_property to cache with classes

2021-10-23 Thread Raymond Hettinger
Raymond Hettinger added the comment: AFAICT, the only capability added by the PR is keeping a weak reference to the instance. This doesn't improve the hit rate and will make the hit rate worse for instances that define __hash__. In the PR's example, two vector instances with equal

[issue45588] cached_method similar to cached_property to cache with classes

2021-10-23 Thread Marten Lienen
Marten Lienen added the comment: However, then the user gets error messages mentioning cached_property when they are actually using cached_method. -- ___ Python tracker ___

[issue45588] cached_method similar to cached_property to cache with classes

2021-10-23 Thread Marten Lienen
Marten Lienen added the comment: An implementation based on cached_property (that therefore also inherits its locking problem) that does not create a circular reference and copies attributes (docs etc.) from the decorated function would be as follows (based on the WeaklyBoundMethod from

[issue45588] cached_method similar to cached_property to cache with classes

2021-10-23 Thread Marten Lienen
Marten Lienen added the comment: @serhiy.storchaka The simple implementation is very simple but does not allow overwriting the arguments to `lru_cache` and, probably more importantly, creates a circular reference on `self`, I believe. -- ___

[issue45588] cached_method similar to cached_property to cache with classes

2021-10-23 Thread Marten Lienen
Marten Lienen added the comment: @AlexWaygood No, I was not aware of the problems. I have updated the PR by removing the lock entirely. Since the function has to be cacheable, it should be idempotent anyway, so that executing it multiple times in parallel does not make a program incorrect.

[issue45588] cached_method similar to cached_property to cache with classes

2021-10-23 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: The simple implementation is: def cached_method(func): return cached_property(lambda self: lru_cache()(partial(func, self))) -- nosy: +serhiy.storchaka ___ Python tracker

[issue45588] cached_method similar to cached_property to cache with classes

2021-10-23 Thread Alex Waygood
Alex Waygood added the comment: Are you aware of the previously reported problems with `cached_property`? https://bugs.python.org/issue43468 -- nosy: +AlexWaygood ___ Python tracker

[issue45588] cached_method similar to cached_property to cache with classes

2021-10-23 Thread Marten Lienen
Change by Marten Lienen : -- keywords: +patch pull_requests: +27462 stage: -> patch review pull_request: https://github.com/python/cpython/pull/29191 ___ Python tracker ___

[issue45588] cached_method similar to cached_property to cache with classes

2021-10-23 Thread Marten Lienen
New submission from Marten Lienen : There should be a `cached_method` equivalent to `cached_property` so that methods of long-lived objects can be cached easily. -- components: Library (Lib) messages: 404870 nosy: martenlienen priority: normal severity: normal status: open title: