| Store | Cart

Re: generator/coroutine terminology

From: Ian Kelly <ian....@gmail.com>
Sat, 14 Mar 2015 14:14:35 -0600
On Sat, Mar 14, 2015 at 1:54 AM, Marko Rauhamaa <mar...@pacujo.net> wrote:
> Steven D'Aprano <stev...@pearwood.info>:>>> Marko Rauhamaa wrote:>>>>> Your 'factory' is a:>>>>>>     generator>>>         A function which returns an iterator.>>>     <URL: https://docs.python.org/3/glossary.html>>>>> That glossary entry is misleading, or at least incomplete, and it>> fails to match the way "generator" is used by actual Python>> programmers.>> I am an actual Python programmer (I develop Python programs) and my> definitive source for Python is the documentation. If there is an error> in the documentation, I would very much like it to be corrected.

I think that Steven was basing his statement on the definition you
cited. I don't think that he actually went and looked up the
definition. If he had, he would have seen that you omitted 90% of it.
Here's the full entry:

"""
A function which returns an iterator. It looks like a normal function
except that it contains yield statements for producing a series of
values usable in a for-loop or that can be retrieved one at a time
with the next() function. Each yield temporarily suspends processing,
remembering the location execution state (including local variables
and pending try-statements). When the generator resumes, it picks-up
where it left-off (in contrast to functions which start fresh on every
invocation).
"""

>> A generator (function) may be a function which returns an iterator,>> but not all functions that return iterators are generators, and in>> some ways returning an iterator is the *least* interesting part of>> what makes a generator a generator.>>>> What distinguishes a generator from a regular function is the use of>> `yield`. Any definition which fails to mention that fact is useless.

As can be seen above, the glossary definition *does* in fact discuss
the use of yield.

> The language reference had better use more precise language. It *is* the> definitive source, after all.

Okay, but you cited the glossary entry, not the language reference.
The language reference says this [1]:

"When a generator function is called, it returns an iterator known as
a generator."

Which I think is quite clear, although to be fair the same section
also uses "generator" alone to refer to the function in the preceding
paragraph. That usage however is justified by PEP 255 [2]:

"""
When a generator function is called, the actual arguments are bound to
function-local formal argument names in the usual way, but no code in
the body of the function is executed.  Instead a generator-iterator
object is returned; this conforms to the iterator protocol, so in
particular can be used in for-loops in a natural way.  Note that when
the intent is clear from context, the unqualified name "generator" may
be used to refer either to a generator-function or a
generator-iterator.

"""

Now which should be considered definitive, the language reference or
the PEP? This question is not rhetorical; I don't know the answer.
Regardless of the answer though, the PEP at least illuminates the
design intent of the terminology.


[1] https://docs.python.org/3/reference/expressions.html#yield-expressions

[2] https://www.python.org/dev/peps/pep-0255/
-- 
https://mail.python.org/mailman/listinfo/python-list

Recent Messages in this Thread
Rustom Mody Mar 12, 2015 01:35 pm
Chris Angelico Mar 12, 2015 01:55 pm
brea...@gmail.com Mar 12, 2015 01:57 pm
Steven DAprano Mar 12, 2015 04:27 pm
Rustom Mody Mar 12, 2015 04:52 pm
Marko Rauhamaa Mar 12, 2015 05:55 pm
Rustom Mody Mar 13, 2015 02:23 am
Steven DAprano Mar 13, 2015 03:30 am
Rustom Mody Mar 13, 2015 05:28 am
Chris Angelico Mar 13, 2015 08:23 am
Rustom Mody Mar 13, 2015 09:12 am
Marko Rauhamaa Mar 13, 2015 09:36 am
Steven DAprano Mar 14, 2015 06:04 am
Marko Rauhamaa Mar 14, 2015 07:54 am
Mark Lawrence Mar 14, 2015 08:04 am
Ian Kelly Mar 14, 2015 08:14 pm
Mark Lawrence Mar 14, 2015 08:31 pm
Rustom Mody Mar 15, 2015 04:15 am
Marko Rauhamaa Mar 14, 2015 08:30 am
Rustom Mody Mar 14, 2015 03:29 pm
Chris Angelico Mar 14, 2015 03:56 pm
Rustom Mody Mar 14, 2015 03:59 pm
Chris Angelico Mar 14, 2015 04:14 pm
Rustom Mody Mar 14, 2015 04:33 pm
Chris Angelico Mar 14, 2015 04:51 pm
Dave Angel Mar 14, 2015 05:07 pm
Mark Lawrence Mar 14, 2015 04:56 pm
Rustom Mody Mar 14, 2015 05:17 pm
Steven DAprano Mar 15, 2015 08:37 am
Chris Angelico Mar 13, 2015 11:32 am
Oscar Benjamin Mar 14, 2015 10:02 pm
Marko Rauhamaa Mar 14, 2015 10:15 pm
Chris Angelico Mar 14, 2015 10:24 pm
Marko Rauhamaa Mar 15, 2015 12:15 am
Chris Angelico Mar 15, 2015 12:22 am
Steven DAprano Mar 16, 2015 01:03 am
Marko Rauhamaa Mar 16, 2015 07:12 am
Chris Angelico Mar 16, 2015 07:21 am
Ian Kelly Mar 16, 2015 07:37 am
Steven DAprano Mar 16, 2015 08:36 am
Chris Angelico Mar 16, 2015 08:58 am
Marko Rauhamaa Mar 16, 2015 12:32 pm
Rustom Mody Mar 16, 2015 12:51 pm
Marko Rauhamaa Mar 16, 2015 01:13 pm
Steven DAprano Mar 16, 2015 02:32 pm
Ian Kelly Mar 16, 2015 02:45 pm
Steven DAprano Mar 16, 2015 01:39 pm
Rustom Mody Mar 16, 2015 02:19 pm
Mark Lawrence Mar 16, 2015 02:26 pm
Steven DAprano Mar 16, 2015 02:35 pm
Steven DAprano Mar 16, 2015 02:36 pm
Rustom Mody Mar 16, 2015 02:37 pm
Rustom Mody Mar 16, 2015 02:55 pm
Mark Lawrence Mar 16, 2015 06:19 pm
Rustom Mody Mar 17, 2015 02:52 am
Mark Lawrence Mar 17, 2015 03:07 am
Rustom Mody Mar 17, 2015 03:18 am
Ian Kelly Mar 16, 2015 02:52 pm
Marko Rauhamaa Mar 16, 2015 03:09 pm
Ian Kelly Mar 16, 2015 03:26 pm
Marko Rauhamaa Mar 16, 2015 04:05 pm
Steven DAprano Mar 16, 2015 11:51 am
Chris Angelico Mar 16, 2015 01:16 pm
Marko Rauhamaa Mar 16, 2015 07:52 am
Steven DAprano Mar 16, 2015 12:02 pm
Jonas Wielicki Mar 16, 2015 12:39 pm
Marko Rauhamaa Mar 16, 2015 12:42 pm
Marko Rauhamaa Mar 16, 2015 07:40 am
Steven DAprano Mar 16, 2015 11:59 am
Marko Rauhamaa Mar 15, 2015 12:48 am
Chris Angelico Mar 15, 2015 02:02 am
Terry Reedy Mar 12, 2015 08:11 pm
Mark Lawrence Mar 17, 2015 03:55 am
Albert van der Horst Mar 31, 2015 12:57 pm
Albert van der Horst Mar 31, 2015 01:18 pm
Dave Angel Mar 31, 2015 01:38 pm
Steven DAprano Apr 03, 2015 06:02 am
Paul Rubin Apr 03, 2015 06:46 am
Albert van der Horst Mar 31, 2015 03:03 pm
Chris Angelico Mar 31, 2015 03:36 pm
Rustom Mody Mar 17, 2015 03:33 am
Mark Lawrence Mar 17, 2015 03:25 am
Marko Rauhamaa Mar 12, 2015 08:22 pm
Messages in this thread