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
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,
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 "
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
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