[Python-ideas] Yield-from: Details to be decided

Arnaud Delobelle arnodel at googlemail.com
Sat Feb 21 20:12:14 CET 2009


2009/2/21 Greg Ewing <greg.ewing at canterbury.ac.nz>:
> Bruce Frederiksen wrote:
>>
>> Actually, PEP 342 specifies that send(None) is like next():
>>
>> "Calling send(None) is exactly equivalent to calling a generator's next()
>> method."
>
> Hmmm, yes, but... that's talking about what happens
> when you call send() on a *generator*.

A generator containing a yield-from expression is still a generator though.

> But when yield-from is delegating to some iterator
> that's not a generator, according the current wording
> in the PEP, things are supposed to behave as though
> you were talking directly to the iterator. If it
> doesn't have a send() method, and you tried to call
> it directly, you would get an exception.

To my eyes, this means that there is an inconsistency between this PEP
and PEP 342.

> We're in unprecedented territory here, and it's hard
> to tell what will turn out to be the most useful
> behaviour without more experience. Raising an
> exception for now seems like the safest thing to
> do.

It will mean that you will need to be aware of the implementation of a
generator in order to know whether it is OK to use send(None) as an
alternative spelling of next().  In some cases it is handy to use
send(None) rather than next, and PEP 342 guarantees that it will work
on generators.  This will break that guarantee.

A way to go round this would be to make the objects returned by
generator functions containing yield-from expressions something else
than generators - maybe 'delegators' ?

-- 
Arnaud



More information about the Python-ideas mailing list