[Python-ideas] generator vs iterator etc. (was: How assignment should work with generators?)

Steven D'Aprano steve at pearwood.info
Tue Nov 28 01:22:39 EST 2017


On Tue, Nov 28, 2017 at 03:11:25PM +0900, Stephen J. Turnbull wrote:
> Steven D'Aprano writes:
> 
>  > The subset of iterators which are created as generators are *also* 
>  > called generators,
> 
> As long as we're being precise, I don't think that is precisely correct:
> 
>     >>> (x for x in range(1))
>     <generator object <genexpr> at 0x10dee5e08>
>     >>> iter(range(1))
>     <range_iterator object at 0x10dab83f0>
>     >>> iter((1,))
>     <tuple_iterator object at 0x10df109b0>
> 
> The two iterators have the same duck-type, the generator is different.

How is the generator different? It quacks like a range_iterator and 
tuple_iterator, it swims like them, it flies like them. Is there some 
iterator method or protocol that generators don't support?


> A generator (object) is, of course, an interable.

And also an iterator:

py> collections.abc
py> isinstance((x+1 for x in range(5)), collections.abc.Iterator)
True


>  > Most of the time the distinction doesn't actually matter, since you
>  > cannot (easily?) create a generator without first creating a
>  > generator function.
> 
> At least you can create a generator (object) with the generator
> function created and called implicitly by using a generator
> expression.

Ah yes, I forget about generator expressions, thanks.



-- 
Steve


More information about the Python-ideas mailing list