[Python-ideas] generator vs iterator etc. (was: How assignment should work with generators?)
Stephen J. Turnbull
turnbull.stephen.fw at u.tsukuba.ac.jp
Tue Nov 28 01:11:25 EST 2017
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.
A generator (object) is, of course, an interable.
> You are right that sometimes the term "generator" is used as
> shorthand for "generator function".
I've always thought "generator factory" would be a better term, but
"generator function" will do. I generally use "generator object" to
make the distinction, though.
> 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.
Reverting from pedantic mode. Hear, hear! this:
> Or if it does matter, it is clear in context which is meant.
>
> For those few times where it *does* matter, there is no substitute for
> precision in language, and that depends on the author, not the
> terminology.
Steve
More information about the Python-ideas
mailing list