questions about functions inside a function
Duncan Booth
duncan.booth at invalid.invalid
Mon Jul 16 10:03:33 EDT 2007
"fdu.xiaojf at gmail.com" <fdu.xiaojf at gmail.com> wrote:
> In order to make sure all variables be larger than 0, I tried to created
> constraint function like this:
>
> cons = []
> for i in range(N):
> c = lambda x: x[i] - 1e-10
> cons.append(c)
>
> But when functions in cons are evaluated, the value in all functions are
> all the same(N-1, which is the last value of i in the loop).
>
> What I want is, when functions in cons be evaluated, every function
> refers to a different variable in X.
So pass i in as a default value:
for i in range(N):
def c(x, i=i):
return x[i] - 1e-10
cons.append(c)
More information about the Python-list
mailing list