> The `i` is the problem.  It's not evaluated when the lambda
> *definition* is executed but when the lambda function is
> called.  And then `i` is always == `n`.  You have to
> explicitly bind it as default value in the lambda definition:
> 
>       polys.append(lambda x, i=i: polys[i](x)*x)
> 
> Then it works.

Just to sate my curiosity, why can the lambda find "polys", but 
not find "i"?  If what you're describing is the case, then it 
seems to me that the following code should work too:

def make_polys(n):
        p = lambda x: 1
        polys = [p]
        for i in range(n):
                p = polys[i]
                polys.append(lambda x: (p(x) * x))
        return polys

yet it suffers the same problem as the original.  Outside the 
scope of make_polys, neither polys[] nor i exists.

There's some subtle behavior here that I'm missing.

-tkc




-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to