I have a class that, let's say, computes a person's insurance risk, and a few other variables are computed during computation. I will need access to the risk and the other variables later.
class InsuranceRiskModel:
self.risk = None
self.other_var = None
...
def get_risk():
# do a bunch of calculations,
# which in the meantime fills out a bunch of other vars
self.other_var = 5
self.risk = 6
return self.risk
def get_other_var():
# risk hasn't been calculated
if not self.risk:
raise NotYetCalculatedError("Not yet calculated!")
return self.other_var
Now in some other function I do:
r = InsuranceRiskModel(person)
risk = r.get_risk()
other_var = r.get_other_var()
Is this a legitimate structure for the sort of program I want? Just throw an exception of the computation hasn't been run, to prevent getting bogus values?