[Python-Dev] accumulator display syntax

Guido van Rossum guido at python.org
Fri Oct 17 14:46:34 EDT 2003

> On Fri, Oct 17, 2003 at 11:55:53AM -0600, Shane Holloway (IEEE) wrote:
> >     mygenerator = x for x in S
> > 
> >     for y in x for x in S:
> >         print y
> > 
> >     return x for x in S
> Interesting but potentially confusing: we could expect the last one
> to mean that we executing 'return' repeatedly, i.e. returning a
> value more than once, which is not what occurs.

I'm not sure what you mean by executing 'return' repeatedly; the
closest thing in Python is returning a sequence, and this is pretty
close (for many practical purposes, returning an iterator is just as
good as returning a sequence).

> Similarily,
>    yield x for x in g()
> in a generator would be quite close to the syntax discussed some
> time ago to yield all the values yielded by a sub-generator g, but
> in your proposal it wouldn't have that meaning: it would only yield
> a single object, which happens to be an iterator with the same
> elements as g().

IMO this is not at all similar to what it suggests for return, as
executing 'yield' multiple times *is* a defined thing.

This is why I'd prefer to require extra parentheses;

  yield (x for x in g())

is pretty clear about how many times yield is executed.

> Even with parenthesis, and assuming a syntax to yield from a
> sub-generator for performance reason, the two syntaxes would be
> dangerously close:
>    yield x for x in g()       # means for x in g(): yield x
>    yield (x for x in g())     # means yield g()

I don't see why we need

  yield x for x in g()

when we can already write

  for x in g():
      yield x

This would be a clear case of "more than one way to do it".

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list