[Python-ideas] Revised**6 PEP on yield-from

Bruce Frederiksen dangyogi at gmail.com
Sun Feb 22 05:02:17 CET 2009


Greg Ewing wrote:
>     * Any values sent to the delegating generator using ``send()``
>       are passed directly to the iterator. If the sent value is None,
>       the iterator's ``next()`` method is called. If the sent value is
>       not None, the iterator's ``send()`` method is called if it has
>       one, otherwise an exception is raised in the delegating generator.
Shouldn't this define which exception is raised?

Also, raising the exception within the delegating generator will (unless 
caught there) finalize the generator.  This may cause surprising results 
if the caller catches the exception and tries to continue to use the 
generator.

Intuitively, I would expect that the delegating generator would not see 
this exception; as if the delegating generator itself lacked a send 
method.  The reasoning is that the error is with the caller and not the 
delegating generator.  Also, given that the send method may work while 
the delegating generator is outside of any yield from, but not work 
during a yield from; not raising the exception within the delegating 
generator gives the caller a safe way to test the waters without 
finalizing the delegating generator.

OTOH, this may be a reason to just translate send to next for non-None 
values too???  Perhaps the justification in that case would be to think 
of it like sending to a yield *statement* (which can't accept the sent 
value) -- which is not an error in generators.

(Is it too late to change my vote for send(non-None) generating an 
exception? :-)
> [snip]
>
> 2. In a generator, the statement
>
> ::
>
>     raise value
>
> is semantically equivalent to
>
>     raise StopIteration(value)
Did you mean "return value" rather than "raise value" here?

-bruce frederiksen



More information about the Python-ideas mailing list