[Tutor] Bitten by lexical closures

2006-05-03 Thread Igor
Hi. And I thought I understood python pretty well. Until I got hit by this: def f(x): ... print x cb = [lambda :f(what) for what in 1234] for c in cb:c() 4 4 4 4 And even this works what = foo for c in cb:c() foo foo foo foo I expected the output to be 1 2 3 4. Now I understand the

Re: [Tutor] Bitten by lexical closures

2006-05-03 Thread Chad Crabtree
Are you just trying to make a continuation? On 5/3/06, Igor [EMAIL PROTECTED] wrote: Hi. And I thought I understood python pretty well. Until I got hit by this: def f(x): ... print x cb = [lambda :f(what) for what in 1234] for c in cb:c() 4 4 4 4 And even this works what =

Re: [Tutor] Bitten by lexical closures

2006-05-03 Thread Python
On Wed, 2006-05-03 at 14:00 +0200, Igor wrote: Hi. And I thought I understood python pretty well. Until I got hit by this: def f(x): ... print x cb = [lambda :f(what) for what in 1234] for c in cb:c() 4 4 4 4 cb = [(lambda x=what:f(x)) for what in 1234] cb[0]() 1 A

Re: [Tutor] Bitten by lexical closures

2006-05-03 Thread Kent Johnson
Igor wrote: Hi. And I thought I understood python pretty well. Until I got hit by this: def f(x): ... print x cb = [lambda :f(what) for what in 1234] for c in cb:c() 4 4 4 4 You haven't actually created a closure because you don't have any nested scopes, you have global scope

Re: [Tutor] Bitten by lexical closures

2006-05-03 Thread Danny Yoo
On Wed, 3 May 2006, Igor wrote: And I thought I understood python pretty well. Until I got hit by this: def f(x): ... print x cb = [lambda :f(what) for what in 1234] for c in cb:c() 4 4 4 4 Hi Igor, I think you're getting caught by something that isn't quite related to closures