[Python-ideas] generator vs iterator etc. (was: How assignment should work with generators?)
Steven D'Aprano
steve at pearwood.info
Mon Nov 27 18:44:49 EST 2017
On Mon, Nov 27, 2017 at 06:35:38PM +0200, Koos Zevenhoven wrote:
> SOLUTION: Maybe (a) all iterators should be called iterators
All iterators *are* called iterators. Just as all mammals are called
"mammals".
The subset of iterators which are created as generators are *also*
called generators, just as the mammals which are dogs are called "dogs"
when it is necessary to distinguish a dog from some other mammal.
> or (b) all
> iterators should be called generators, regardless of whether they are
> somehow a result of a generator function having been called in the past.
Absolutely not. That would be confusing -- it would be analogous to
calling all sequences (lists, tuples, deques etc) "strings". What
benefit is there to calling all iterators "generators", losing the
distinction between those which are defined using def and yield and
those created using iter()? Sometimes that distinction is important.
You are right that sometimes the term "generator" is used as shorthand
for "generator function". Most of the time the distinction doesn't
actually matter, since you cannot (easily?) create a generator without
first creating a generator function.
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