list of polynomial functions
Fredrik Lundh
fredrik at pythonware.com
Thu Jun 15 14:45:23 EDT 2006
Tim Chase wrote:
>> 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:
it's not that it cannot find it, it's that if you use a closure, i will
have the same value for all lambdas.
> There's some subtle behavior here that I'm missing.
lexical closures bind to names, default arguments bind to values.
</F>
More information about the Python-list
mailing list