cached-property
cached-property copied to clipboard
`x = cached_property(y)` doesn't work properly
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.
I did a dirty workaround seems like it works if you do this:
x_len = cached_property(get_x_len).func
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.
I did a dirty workaround seems like it works if you do this:
x_len = cached_property(get_x_len).func
x_lenshould 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.