I have a model with a calculated value that is expensive to calculate. I need to call the results in several places that's why on the first call I want to calculate this value and then return the calculated value assuming other values do not change.
My own solution is to have an internal attribute that is set the first time the property method is called:
from pydantic import BaseModel
class MyModel(BaseModel):
value1: int
_value2: int
@property
def value2(self):
if not hasattr(self, '_value2'):
print('calculated result')
self._value2 = self.value1*3
return self._value2
This would work with a regular class, but with a pydantic model this fails:
>> mm = MyModel(value1=2)
>> mm.value2
calculated result
ValueError: "MyModel" object has no field "_value2"