【发布时间】:2017-07-13 23:25:54
【问题描述】:
我正在尝试计算一次值(因为它需要很长时间)然后存储该值以便再次使用它。
我知道 types.MethodType 但我只想引用一个属性而不必调用它。
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
标签: python python-3.x properties