[Python-ideas] PEP 479: Change StopIteration handling inside generators

Guido van Rossum guido at python.org
Mon Nov 24 19:25:04 CET 2014


On Mon, Nov 24, 2014 at 10:02 AM, Alexander Belopolsky <
alexander.belopolsky at gmail.com> wrote:

>
> On Mon, Nov 24, 2014 at 12:23 PM, Chris Angelico <rosuav at gmail.com> wrote:
>
>> Well, there is probably more to be said about this - along the lines
>> of *why* generators ought to be more like iterators. (They're
>> iterables, not iterators.)
>>
>
> If generators are not iterators, how do you explain this?
>
> >>> import collections
> >>> def g():
> ...     yield 42
> ...
> >>> isinstance(g(), collections.Iterator)
> True
>

I think Chris A was overzealous here. The word "generator" is ambiguous; it
can refer to either a generator function (a function definition containing
at least one "yield") or to the object you obtain by calling a generator
function. The latter is definitely an iterator (it has a __next__ method).
You can't really call a generator function an iterable (since calling
iter() on it raises TypeError) but it's not an iterator either. For the
rest see my explanation in response to Mark Shannon in python-dev:
http://code.activestate.com/lists/python-dev/133428/

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20141124/aca5631e/attachment.html>


More information about the Python-ideas mailing list