I'm writing some scientific code in Python which solves a complicated optimization problem and (ideally) returns a list of univariate functions which are a series of solution to the problem at particular points in time (a list of time-indexed, univariate policy functions, if you will).
The list of functions needs to be independent from the object which creates it. This is where I hit trouble -- inevitably, each function in the list needs to reference a value which was solved-for and stored in a list in the solver object.
Here's a quick little program that illustrates the problem I'm running into:
class test:
def __init__(self):
self.vals = np.arange(5)
def testfxns(self):
fxns = {}
for i in range(len(self.vals)):
def temp_func(x):
Z = self.vals[i]
return Z + x
fxns[i] = temp_func
#del temp_func # doesn't help
return fxns
t1 = test()
z = t1.testfxns()
print z[0](2)
print z[1](2)
print z[2](2)
print z[3](2)
print z[4](2)
The output of the print statements is all 6's -- I'd like it to be 2, 3, 4, 5, 6.
Is there any way to address this? I need those constants in the "Z" variable to be inserted into each iteration of the list of functions. The "poor man's" solution would be to print out each constant value and then just manually type out the list of functions, manually typing the constant values I need into different individual function definitions.
Is there any way to obtain the value of "self.vals[i]" in a "dereferenced" way, programatically? (Hopefully that makes sense.) Or alternatively, what's the terminology related to even solving this issue -- what terms should I be googling and/or researching in the documentation? (Some of the trouble is not even knowing where to be looking.)
Thanks so much. I know there may be multiple issues I'm running into here, so all help/guidance is appreciated.