I'm trying to calculate a value once (because it takes a long time) and then store the value so that it can be used again.
I am aware types.MethodType but I want to just reference a property and not have to call it.
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)
Error Message:
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