Jun-09-2020, 04:48 PM
Another way, keep track of the previous values, only re calculate if they have changed.
class Vessel(): def __init__(self, class_notation, length): self.class_notation = class_notation self.length = length self.previous_values = (None, None, None) @property def wave_coef(self): if self.previous_values[:2] == (self.class_notation, self.length): return self.previous_values[2] print('calculating wave_coef') table = {"R0": 1, "R1": 0.9, "R2": 0.8, "R3": 0.7, "R4": 0.6, "R5": 0.5, "R6": 0.4} service_factor = table[self.class_notation] if (self.length <= 100): CW = 0.08 * self.length * service_factor else: CW = 6 + 0.02 * self.length * service_factor self.previous_values = (self.class_notation, self.length, CW) return CW vessel = Vessel('R2', 2) print(vessel.wave_coef) print(vessel.wave_coef) vessel.class_notation = 'R4' print(vessel.wave_coef) vessel.length = 1 print(vessel.wave_coef)
Output:calculating wave_coef
0.128
0.128
calculating wave_coef
0.096
calculating wave_coef
0.048