Merging a generator's "next" and "send" methods?
Since .next() seems to be equivalent to .send(None), why wasn't .next() just given an optional parameter which defaults to None? - Andrey On Thu, Oct 1, 2009 at 1:10 PM, Simon Forman <sajmikins@gmail.com> wrote:
On Wed, Sep 30, 2009 at 4:24 PM, Andrey Fedorov <anfedorov@gmail.com> wrote:
As far as I can tell, a generator's .next() is equivalent to .send(None). Is this true?
They are equivalent AFAIK.
If so, [why] aren't they unified in a method with a single argument which defaults to None? - Andrey
next() predates send().
On Fri, Feb 19, 2010 at 11:12 AM, Andrey Fedorov <anfedorov@gmail.com> wrote:
Since .next() seems to be equivalent to .send(None), why wasn't .next() just given an optional parameter which defaults to None?
Because for built-in iterator types the next() method is implemented in C and we didn't want to invalidate existing C code by giving it an optional argument. But why do you care? --Guido
- Andrey On Thu, Oct 1, 2009 at 1:10 PM, Simon Forman <sajmikins@gmail.com> wrote:
On Wed, Sep 30, 2009 at 4:24 PM, Andrey Fedorov <anfedorov@gmail.com> wrote:
As far as I can tell, a generator's .next() is equivalent to .send(None). Is this true?
They are equivalent AFAIK.
If so, [why] aren't they unified in a method with a single argument which defaults to None? - Andrey
next() predates send().
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
-- --Guido van Rossum (python.org/~guido)
On 19 Feb 2010, at 17:12 , Andrey Fedorov wrote:
Since .next() seems to be equivalent to .send(None), why wasn't .next() just given an optional parameter which defaults to None?
- Andrey
You'd have to check the ML discussions on PEP 342 (Coroutines via Enhanced Generators). The PEP explicitly states that send(None) is equivalent to next()
A new method for generator-iterators is proposed, called send(). It takes exactly one argument, which is the value that should be "sent in" to the generator. Calling send(None) is exactly equivalent to calling a generator's next() method. Calling send() with any other value is the same, except that the value produced by the generator's current yield expression will be different.
A new method was probably created because it would be quite unclear that `next(value)` sends that value into the generator for consumption by a yield expression. Whereas the purpose of `send(value)` looks pretty obvious.
Guido van Rossum <guido@python.org> wrote:
Because for built-in iterator types the next() method is implemented in C and we didn't want to invalidate existing C code by giving it an optional argument.
Ah, makes sense. But why do you care? Curiosity more than anything. It would be elegant if .send() had identical behavior to .next() without an argument, instead of throwing an exception, but it's no biggie. Cheers, Andrey Masklinn <masklinn@masklinn.net> wrote:
You'd have to check the ML discussions on PEP 342 (Coroutines via Enhanced Generators).
Sweet, thanks for the pointer. Sorry for not searching harder. On Fri, Feb 19, 2010 at 11:32 AM, Masklinn <masklinn@masklinn.net> wrote:
On 19 Feb 2010, at 17:12 , Andrey Fedorov wrote:
Since .next() seems to be equivalent to .send(None), why wasn't .next()
just
given an optional parameter which defaults to None?
- Andrey You'd have to check the ML discussions on PEP 342 (Coroutines via Enhanced Generators). The PEP explicitly states that send(None) is equivalent to next()
A new method for generator-iterators is proposed, called send(). It takes exactly one argument, which is the value that should be "sent in" to the generator. Calling send(None) is exactly equivalent to calling a generator's next() method. Calling send() with any other value is the same, except that the value produced by the generator's current yield expression will be different.
A new method was probably created because it would be quite unclear that `next(value)` sends that value into the generator for consumption by a yield expression. Whereas the purpose of `send(value)` looks pretty obvious.
participants (3)
-
Andrey Fedorov
-
Guido van Rossum
-
Masklinn