-2
我試圖計算一次值(因爲它需要很長時間),然後存儲該值,以便它可以再次使用。
我知道types.MethodType,但我只想引用一個屬性,而不必調用它。重新分配@property方法python到對象
import types
class stuff:
@property
def compute_once(self):
takes_long_time_to_calculate = 5 - 2
self.compute_once = takes_long_time_to_calculate
return takes_long_time_to_calculate
instance = stuff()
print(instance.compute_once)
print(instance.compute_once)
錯誤消息:
Traceback (most recent call last):
File "try.py", line 12, in <module>
print(instance.compute_once)
File "try.py", line 7, in compute_once
self.compute_once = takes_long_time_to_calculate
AttributeError: can't set attribute
我意識到這不是很漂亮,但是idk –