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

1 view
Skip to first unread message

Greg Ewing

unread,
Feb 20, 2009, 5:15:59 PM2/20/09
to Python-Ideas
I've got to the point in the implementation where I
need to decide what to do if you send() a value to
a generator that's delegating to something that
doesn't have a send() method.

Possibilities include:

* Ignore the value and call next() instead

* Raise an exception

What do people think? I'm inclined to raise an
exception for the time being, since we can always
relax it later if we want. Also, doing so is more
consistent with the idea of the caller talking
directly to the sub-iterator.

--
Greg
_______________________________________________
Python-ideas mailing list
Python...@python.org
http://mail.python.org/mailman/listinfo/python-ideas

Jesse Noller

unread,
Feb 20, 2009, 5:15:18 PM2/20/09
to Greg Ewing, Python-Ideas
Raising an exception seems more sane right now

On Feb 20, 2009, at 5:15 PM, Greg Ewing <greg....@canterbury.ac.nz>
wrote:

Guido van Rossum

unread,
Feb 20, 2009, 5:30:13 PM2/20/09
to Greg Ewing, Python-Ideas
In general .send() is picky when it knows for sure the value won't be
used -- try .send() on a generator suspended before the first time it
yields, that raises an exception too. So yes, an exception, please.
(Doesn't the PEP specify this? I told you it would be useful to start
coding. :-))

--Guido

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

Bruce Frederiksen

unread,
Feb 20, 2009, 5:31:14 PM2/20/09
to Greg Ewing, Python-Ideas
Raise an exception.

-bruce frederiksen

Greg Ewing wrote:
> I've got to the point in the implementation where I
> need to decide what to do if you send() a value to
> a generator that's delegating to something that
> doesn't have a send() method.
>
> Possibilities include:
>
> * Ignore the value and call next() instead
>
> * Raise an exception
>
> What do people think? I'm inclined to raise an
> exception for the time being, since we can always
> relax it later if we want. Also, doing so is more
> consistent with the idea of the caller talking
> directly to the sub-iterator.
>

_______________________________________________

Bruce Frederiksen

unread,
Feb 20, 2009, 5:46:18 PM2/20/09
to Greg Ewing, Python-Ideas
Actually, PEP 342 specifies that send(None) is like next():

"Calling send(None) is exactly equivalent to calling a generator's
next() method."

So to honor this, you would need to have send(None) call next, while
send(anything_else) raises an exception...

-bruce frederiksen

Greg Ewing wrote:
> I've got to the point in the implementation where I
> need to decide what to do if you send() a value to
> a generator that's delegating to something that
> doesn't have a send() method.
>
> Possibilities include:
>
> * Ignore the value and call next() instead
>
> * Raise an exception
>
> What do people think? I'm inclined to raise an
> exception for the time being, since we can always
> relax it later if we want. Also, doing so is more
> consistent with the idea of the caller talking
> directly to the sub-iterator.
>

_______________________________________________

Guido van Rossum

unread,
Feb 20, 2009, 5:49:44 PM2/20/09
to Bruce Frederiksen, Python-Ideas
Good point.

--

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

Greg Ewing

unread,
Feb 20, 2009, 11:09:01 PM2/20/09
to Python-Ideas
Guido van Rossum wrote:
> So yes, an exception, please.
> (Doesn't the PEP specify this?

It's remarked upon as an unresolved issue in the PEP
right now. I'll change it to specify an exception if
you think that's the right thing to do.

Greg Ewing

unread,
Feb 21, 2009, 1:09:51 AM2/21/09
to Python-Ideas
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*.

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.

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.

--
Greg

Bruce Frederiksen

unread,
Feb 21, 2009, 8:50:35 AM2/21/09
to Greg Ewing, Python-Ideas
Greg Ewing wrote:
> 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*.
OK, that argument makes sense. And I can't think of any
counter-examples off of the top of my head where send(None) needs to
call next...

-bruce frederiksen

Guido van Rossum

unread,
Feb 21, 2009, 11:31:21 AM2/21/09
to Greg Ewing, Python-Ideas
On Fri, Feb 20, 2009 at 10:09 PM, Greg Ewing
<greg....@canterbury.ac.nz> wrote:
> 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*.
>
> 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.
>
> 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.

Agreed after all.

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

Arnaud Delobelle

unread,
Feb 21, 2009, 2:12:14 PM2/21/09
to Greg Ewing, Python-Ideas
2009/2/21 Greg Ewing <greg....@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

Greg Ewing

unread,
Feb 21, 2009, 5:28:16 PM2/21/09
to Python-Ideas
Arnaud Delobelle wrote:

> 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().

Yes, and I've now decided that send(None) will be
converted to next() upon delegation in all cases.

I'm no longer going to describe the semantics in
terms of "direct communication", since that's not
exactly true any more (and probably never really
was).

--
Greg

Reply all
Reply to author
Forward
0 new messages