I have an iterative algorithm with values specific to each iteration. Both sub-algorithms could use (and modify) any of the computed state variables up to the point at which they are called.
My current approach is to pass the whole self
to the strategies, but this annoys me as it also exposes some yet to be computed values (in the iteration sense; the value computed during iteration i
does not count at iteration i
+1, etc.) (do we call these lazy variables?).
Ideally, the strategies should only know about the dependencies and the set state variables (with typing of course).
Do you have better ideas?
class MyClass:
def __init__(self, dep1: Dep1, dep2: Dep2, algo1: Algo1, algo2: Algo2):
self.dep1 = dep1
self.dep2 = dep2
self.algo1 = algo1
self.algo2 = algo2
self.state_var1: T1
self.state_var2: T2
self.state_var3: T3
def __call__(self, n: int):
for i in range(n):
# compute state_var1
# compute state_var2
self.algo1(self)
# compute state_var3
self.algo2(self)
# etc
An option that I explored is the creation of two protocols (+ one for dependencies) that describe the state at each relevant point
class State1(Dependencies, Protocol):
state_var1: T1
state_var2: T2
class State2(Dependencies, Protocol):
state_var3: T3
Then algo1's signature will depend on State1
, and algo2's on State2
.
class Algo1(Protocol):
def __call__(self, state: State1): ...