Closed Atry closed 11 months ago
Big thanks for the report! Unlike classmethod
, cached_property
needs to be assigned after its first use which makes it tough for types. I'll think of a solution today.
I think cached_property
should check and update the class's own __dict__
instead of attributes in super classes.
Hm... I have looked into it. I am afraid there's no way to keep both the efficiency of cached_property
and the consistent subclassing behavior of classmethod
. We could add a _cached_value
attribute and create a new instance of cached_classproperty
on a __get__
from the new owner with an empty _cached_value
but it's a lot slower than the current solution.
Do you have any suggestions?
In other languages, like Hack, they provide two different implementations called Memorize and Memorize LSB respectively https://docs.hhvm.com/hack/attributes/predefined-attributes#__memoizelsb
On Sat, Sep 23, 2023 at 3:36 AM Stanislav Zmiev @.***> wrote:
Hm... I have looked into it. I am afraid there's no way to keep both the efficiency of cached_property and the consistent subclassing behavior of classmethod. We could store _instance attribute, cache it instead, and return it on every next invocation. However, I feel like this is going to be much slower than the current solution.
But I also don't want to prematurely optimize. So I'll make a pull request with the proposed solution and send it to you for review. If you can help me come up with a better one -- great. If not, we'll contemplate whether it makes sense to change behavior, probably by making a small benchmark.
— Reply to this email directly, view it on GitHub https://github.com/Ovsyanka83/cached_classproperty/issues/1#issuecomment-1732278126, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAES3OXLGUD3HMXASYT3IC3X323UVANCNFSM6AAAAAA5D2UAPY . You are receiving this because you authored the thread.Message ID: @.***>
Makes sense. The LSB version is still not going to be as fast but it can work. I'll prepare a PR soon
I think they can be named cached_classproperty
and cached_staticproperty
, respectively. cached_staticproperty
should not have the cls
parameter to ensure it would not be misused.
@Atry please, see if the following implementation makes sense: https://github.com/Ovsyanka83/cached_classproperty/pull/2
Describe the bug
cached_classproperty
can result in surprising result in inherited classesTo Reproduce
Output:
Expected behavior
my_cached_classproperty
should return the result same asmy_classmethod()
Additional context