Christian Wyglendowski wrote:
>>-----Original Message-----
>>From: [EMAIL PROTECTED] 
>> If I have this code:
<snip>

>> Obviously, the lambda is using "value" at the end of the loop (4),
>>rather than what I want, "value" during the loop (0,1,2,3).  
> 
> Right.  I think the issue is that your lambda calls another funtion.
> However, the function isn't called until the lambda is called later,
> when value == 4.

No, the problem is not when the function is called, but when value is bound 
into the closure.

> I'd use a closure rather than a lambda. 

The original solution does use a closure. The problem is that variables are not 
bound into a closure until the scope of the variable exits. That is why using a 
separate factory function works - the closure is bound when the factory 
function exits which happens each time through the loop. In the case of 
closures in a loop the closure is not bound until exiting the scope containing 
the loop and all the closures are bound to the same value.

Kent
-- 
http://www.kentsjohnson.com

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to