[Python-Dev] generator comprehension syntax, was: accumulator display syntax

Phillip J. Eby pje at telecommunity.com
Fri Oct 17 23:28:39 EDT 2003

At 09:10 PM 10/17/03 -0400, Raymond Hettinger wrote:
> > I'd just like to pipe into this discussion saying that while Peter
> > Norvig's pre-PEP is neat, I'd reject it if it were a PEP; the main
> > reason that the proposed notation doesn't return a list.  I agree that
> > having generator comprehensions would be a more general solution.  I
> > don't have a proposal for generator comprehension syntax though, and
> > [yield ...] has the same problem.
>Is Phil's syntax acceptable to everyone?
>      (yield:  x*x for x in roots)

Ironically, I'm opposed.  :)

* Yield is a control flow statement, this is an expression

* yield: looks like lambda, and this is not a function

* Yield only makes sense if you come into this thinking about generators

* Yield distracts from the purpose of the expression

To put it another way, Python is "executable pseudocode".  Listcomps are 
pseudocode.  Yield in a generator is pseudocode.  (x*x for x in roots) is 
pseudocode.  But (yield: x*x for x in roots) looks like some kind of weird 
programming language gibberish.  :)

I think the worst misinterpretation I could have about the yield-less 
syntax is that I might think it was a "tuple comprehension" or something 
that returned a sequence instead of an iterator.  However, I'll find out 
it's not a sequence or tuple if I try to do anything with it that requires 
a sequence or tuple.  My worst case problem is re-execution of the iterator.

Which, by the way, brings up a question: should iterator comps be 
reiterable?  I don't see any reason right now why they shouldn't be, and 
can think of situations where reiterability would be useful.

More information about the Python-Dev mailing list