[Python-Dev] PEP 289 - Generator Expressions

Jewett, Jim J jim.jewett at eds.com
Wed May 5 14:31:01 EDT 2004


Guido van Rossum:
>... mostly-late binding -- which is late binding, except that 
> the iterable in the leftmost for-clause is evaluated at once. 

Did you really mean leftmost, or all-but-the-rightmost?  

Special-casing the left-most causes the two-generator version
to behave as expected, but it makes the three-generator case
even more confusing.

Perhaps the entire early/late binding problem comes from trying 
to treat multiple generators as a special case.

For a single-generator expression, late binding makes sense
(or why use a free variable), but early binding would also
work most of the time.

For multiple generators, the current behavior is that the
final generator is exhausted by the first loop, so the other
generators really only need one value each.  The only problem
with that is that it isn't usually what people want.

If users do want a cross-product, then either the values have 
to be stored somewhere, or python has to create a whole series 
of "equivalent" generators.  Python can't do this any better
than the user, so why not make the storage explicit?  

	(x,y,z for x in e1 for y in e2 for z in e3)

<==>

	for x in e1.__iter__():
		for x in e2.__iter__()
			for z in e3.__iter__()
				yield (x,y,z)

It is the user's responsibility to ensure that e2 and e3 can
return an iterator multiple times, perhaps by wrapping them
in a list or tuple.

-jJ



More information about the Python-Dev mailing list