Author rhettinger
Recipients Yonatan Goldschmidt, rhettinger
Date 2020-10-23.21:41:48
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1603489308.52.0.995877475391.issue42127@roundup.psfhosted.org>
In-reply-to
Content
> Essentially it means that types using cached_property are less
> likely to enjoy the benefits of shared keys.

I don't think anything can be done about it.  @cached_property and key-sharing dicts are intrinsically at odds with one another.  Likewise, @cached_property doesn't work with classes that define __slots__.

FWIW, there is an alternative that works with both key-sharing dicts and __slots__.  You can stack property() on top of functools.cache():

    class A:
        def __init__(self, x):
                self.x = x

        @property
        @cache
        def square(self):
                print('Called!')
                return self.x ** 2

            
    >>> a = A(10)
    >>> a.square
    Called!
    100
    >>> b = A(11)
    >>> b.square
    Called
    121
    >>> a.square
    100
    >>> b.square
    121
History
Date User Action Args
2020-10-23 21:41:48rhettingersetrecipients: + rhettinger, Yonatan Goldschmidt
2020-10-23 21:41:48rhettingersetmessageid: <1603489308.52.0.995877475391.issue42127@roundup.psfhosted.org>
2020-10-23 21:41:48rhettingerlinkissue42127 messages
2020-10-23 21:41:48rhettingercreate