[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 abo

[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 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-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 <https://bugs.python.org/issue45

[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 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 <https://bugs.python.org/issu

[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