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

Chris Angelico rosuav at gmail.com
Mon Nov 24 23:48:15 CET 2014


On Tue, Nov 25, 2014 at 5: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

My apologies. As Guido said, "generator" is ambiguous; though I was
inaccurate as well. A generator *object* is, as you show above, an
iterator; a generator *function* is not actually iterable, but it is
an iterator factory. An iterator class is also an iterator factory.

ChrisA


More information about the Python-ideas mailing list