[Python-Dev] 2.5 and beyond (original) (raw)
Tim Peters tim.peters at gmail.com
Sat Jul 1 02:15:51 CEST 2006
- Previous message: [Python-Dev] 2.5 and beyond
- Next message: [Python-Dev] 2.5 and beyond
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
[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.
- Previous message: [Python-Dev] 2.5 and beyond
- Next message: [Python-Dev] 2.5 and beyond
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]