RELEASED Python 2.4, alpha 1

Tim Peters tim.peters at gmail.com
Fri Jul 9 16:56:06 EDT 2004


[Michele Simionato]
> Uhm ... I see generator expressions have late bindings, just as list
> comprehensions:
> 
> >>> f1,f2,f3=tuple(lambda : i for i in [1,2,3])
> >>> f1()
> 3
> >>> f2()
> 3
> >>> f3()
> 3
> 
> I was more in the camp of early bindings; I would like to know if late
> bindings are final or subject to changes and it there a pronouncement
> from Guido.

Guido Pronounced:  the expression in the leftmost "for" clause is
evaluated immediately, but all the rest is delayed.  So in your
example, only "[1, 2, 3]" is evaluated at the time the genexp is
created.  If you had tried to iterate instead over, say, range(1/0),
the ZeroDivisionError would have been raised immediately, which is the
real point of evaluating that one piece "early".

Don't ask me to justify the rest <wink>.

Guido doesn't really care about examples like yours.  He thinks
genexps will overwhelmingly be consumed "on the same line" they're
created, and that people doing fancy-pants stuff like you're doing
there probably shouldn't (but could find more-or-less obvious
workarounds if they had to, given that they're obsessed enough to try
such fancy-pants stuff to begin with).

The world won't end either way (IMO), and (also IMO) Python has pushed
delayed code blocks in a scoped language without explicit scope
declarations about as far as it can without becoming plainly
incomprehensible.



More information about the Python-list mailing list