cached-property icon indicating copy to clipboard operation
cached-property copied to clipboard

`x = cached_property(y)` doesn't work properly

Open VadimPushtaev opened this issue 7 years ago • 3 comments

from cached_property import cached_property


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

    def get_x_len(self):
        print('...')
        return len(self._x)

    x_len = cached_property(get_x_len)


a = A([1, 2, 3])
print(a.get_x_len())
print(a.x_len)
print(a.get_x_len())

This examples fails with TypeError: 'int' object is not callable. The problem is, cached_property replaces get_x_len with 3 while it actually semantically should replacing x_len with 3.

VadimPushtaev avatar Oct 10 '18 22:10 VadimPushtaev

I did a dirty workaround seems like it works if you do this:

x_len = cached_property(get_x_len).func

roelzkie15 avatar Apr 08 '21 17:04 roelzkie15

I did a dirty workaround seems like it works if you do this:

x_len = cached_property(get_x_len).func

x_len should be a property, not a method.

VadimPushtaev avatar Apr 14 '21 13:04 VadimPushtaev

I did a dirty workaround seems like it works if you do this:

x_len = cached_property(get_x_len).func

x_len should be a property, not a method.

Hmmm i think you are right, the cached_property(get_x_len).func returned 3 instead of a property.

roelzkie15 avatar Apr 14 '21 16:04 roelzkie15