[Python-ideas] Proto-PEP on a 'yield from' statement

Bruce Leban bruce at leapyear.org
Fri Feb 13 08:17:12 CET 2009


On Thu, Feb 12, 2009 at 10:27 PM, Bruce Frederiksen <dangyogi at gmail.com>wrote:

> Raymond Hettinger wrote:
>
>> I recommend dropping the notion of forwarding from the proposal.
>> The idea is use-case challenged, complicated, and should not be
>> hidden behind new syntax.
>>
>> Would hate for this to become a trojan horse proposal
>> when most folks just want a fast iterator pass-through mechasism:
>>
> I don't really understand your objection.  How does adding the ability to
> forward send/throw values and closing the subgenerator in any way whatsoever
> get in the way of you using this as a fast iterator pass-through mechanism?
>
> I agree that 98% of the time the simple pass-through mechanism is all that
> will be required of this new feature.  And I agree that this alone is
> sufficient motivation to want to see this feature added.  But I have done
> quite a bit of work with nested generators and end up having to use
> itertools.chain, which also doesn't support the full generator behavior.
> <snip>


One of the advantages of full support of generators in a new 'yield from' is
future proofing. Given the statement:

The effect is to run the iterator to exhaustion,
during which time it behaves as though it were communicating directly
with the caller of the generator containing the ``yield from`` expression
(the "delegating generator").

If, hypothetically, Python were to add some new feature to generators then
those would automatically work in this context. Every place in current code
which implements this kind of mechanism is not future proof.

I didn't follow all the variations on the for loop, but regarding send, it
seems to me that a natural case is this:

    for x in foo:
        bar = process(x)
        foo.send(bar)

which sends the value bar to the generator and the value that comes back is
used in the next iteration of the loop. I know that what I wrote doesn't do
that so what I really mean is something like this but easier to write:

    try:
        x = foo.next()
        while True:
            bar = process(x)
            x = foo.send(bar)
    except StopIteration:
        pass

and the syntax that occurs to me is:

    for x in foo:
        bar = process(x)
        continue bar

As to chaining generators, I don't see that as a for loop-specific feature.
If that's useful in a for, then it's useful outside and should stand on its
own. (And I withhold judgment on that as I don't yet see a benefit to the
new syntax vs. what's available today.)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20090212/ce78f388/attachment.html>


More information about the Python-ideas mailing list