[Giovanni Bajo]
> Yes but:
>
> >>> a = []
> >>> for i in range(10):
> ...     a.append(lambda: i)
> ...
> >>> print [x() for x in a]
> [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
>
> This subtle semantic of lambda is quite confusing, and still forces people to
> use the "i=i" trick.

So stay away from excruciating abuses of lexical scoping you don't
understand ;-)  What do you _expect_ `i` to refer to?  "Oh, it should
guess that I didn't really mean to defer evaluation of the lambda body
at all, but instead evaluate the lambda body at the time I define the
lambda and then synthesize some other function that captures the
specific outer bindings in effect at lambda-definition time" doesn't
really cut it.

Try spelling what you think you want here in Scheme.  Before it works,
you'll probably end up with some equally "atrocious" (let ((i i)) ...)
gimmick to force capturing each binding for `i` as it flies by.  Else
Scheme will also use the outer binding for `i` in effect at the time
the lambdas are _executed_.

This isn't typical use for lambda, and I don't think it's what Andrew
had in mind.
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to