[Python-Dev] PEP 380 (yield from a subgenerator) comments

Guido van Rossum guido at python.org
Fri Mar 27 04:39:48 CET 2009

On Thu, Mar 26, 2009 at 4:19 PM, P.J. Eby <pje at telecommunity.com> wrote:
> At 12:27 PM 3/26/2009 -0700, Guido van Rossum wrote:
>> There is some clear low-hanging fruit for Greg's proposal
>> where no trampoline or helpers are needed -- but where currently
>> refactoring complex code containing many yield statements is
>> cumbersome due to the nee to write each "subroutine" call as "for x in
>> subroutine(): yield x" -- being able to replace this with "yield from
>> subroutine()" is a conceptual advantage to me that is not proportional
>> to the number of characters saved.
> Right - I don't object to the concept of "yield from" -- I'm -0 on that.
>  What I don't like is the confusion of adding "return values" to generators,
> at least using the 'return' statement.

Well, after thinking about it some more I think I am going to have to
take an executive decision and override your opposition. :-)

I wasn't keen on this myself at the outset, but it's growing on me,
and I am finding Greg's motivation for wanting to do it this way
pretty strong. I'm +1 on "yield from" and +0 on return values in

That +0 could turn into a +1 if there was a way to flag this as an
error (at runtime), at least if the return is actually executed:

def g():
    yield 42
    return 43

for x in g():
    print x    # probably expected to print 42 and then 43

Perhaps the exception used in this case could be a different exception
than StopIteration? Regular iteration could either just pass this
exception through or explicitly check for it (a single pointer
comparison could usually suffice), depending on whether it would be a
subclass of StopIteration.

Greg, what do you think? (There could be good reasons not to
complexificate it this way too.)

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

More information about the Python-Dev mailing list