[Python-ideas] Introducing where clauses

Stephen J. Turnbull stephen at xemacs.org
Mon Jun 22 16:20:11 CEST 2009


Andrey Popp writes:

 > Your statement:
 > 
 >     [(y, y) for y in (f(x) for x in some_iterable) if y < 2]
 > 
 > means producing a list by iterating over the generator, which iterates
 > over the some_iterable, it is correct in algorithmic way, but not in
 > semantic.

That's only true if you think of "iterable" as a generalized (ie,
possibly infinite) sequence.  However, you can also think of this, not
as "iterating over a sequence of values created by iterating over
another sequence of values," but rather "iterating 'application' over
a sequence of filters."  Then the "for" clause functions as a "where".
The first "for" in Ben's expression is interpreted as "where y = f(x)",
and the second "for" is interpreted as "where x = next(some_iterable)".

This is not fully general; it only works at all in a comprehension
context, and I'm not entirely sure it works perfectly here.  But it
troubles me that we already have a way to say "where" at the upper
levels of nesting, and you want to introduce a new "where" that is
only useful at upper levels of nesting.

Another way to put this is that because these are iterables (streams)
rather than sequences (ordered (finite) sets), it doesn't make sense
to talk about "double interation."  There are multiple levels of
iterable here, but in the end there's only one iterative process, and
all the levels share the same "iteration".



More information about the Python-ideas mailing list