【发布时间】:2019-03-26 13:10:57
【问题描述】:
我有以下class,缓存property:
class Object:
def __init__(self, var):
self._var = var
@property
@lru_cache()
def some_property(self):
print("i did some calculation")
return self._var + 3
>> obj = Object(3)
>> obj.some_property
i did some calculation
6
我怎样才能做到这样每当我用相同的var创建一个新的Object时,它不会重新计算,而是memoize结果胖class level而不是重新计算somme_property。
换句话说,我需要它这样做:
>> new_obj = Object(3)
>> new_obj.some_property
6
【问题讨论】:
标签: python-3.x class caching memoization