[Python-ideas] generator vs iterator etc. (was: How assignment should work with generators?)
Koos Zevenhoven
k7hoven at gmail.com
Mon Nov 27 11:35:38 EST 2017
On Mon, Nov 27, 2017 at 3:55 PM, Steven D'Aprano <steve at pearwood.info>
wrote:
> On Mon, Nov 27, 2017 at 12:17:31PM +0300, Kirill Balunov wrote:
>
>
> > 2. Should this work only for generators or for any iterators?
>
> I don't understand why you are even considering singling out *only*
> generators. A generator is a particular implementation of an iterator. I
> can write:
>
> def gen():
> yield 1; yield 2; yield 3
>
> it = gen()
>
> or I can write:
>
> it = iter([1, 2, 3])
>
> and the behaviour of `it` should be identical.
>
>
>
I can see where this is coming from. The thing is that "iterator" and
"generator" are mostly synonymous, except two things:
(1) Generators are iterators that are produced by a generator function
(2) Generator functions are sometimes referred to as just "generators"
The concept of "generator" thus overlaps with both "iterator" and
"generator function".
Then there's also "iterator" and "iterable", which are two different things:
(3) If `obj` is an *iterable*, then `it = iter(obj)` is an *iterator* (over
the contents of `obj`)
(
4) Iterators yield values, for example on explicit calls to next(it).
Personally I have leaned towards keeping a clear distinction between
"generator function" and "generator", which leads to the situation that
"generator" and "iterator" are mostly synonymous for me. Sometimes, for
convenience, I use the term "generator" to refer to "iterators" more
generally. This further seems to have a minor benefit that "generators" and
"iterables" are less easily confused with each other than "iterators" and
"iterables".
I thought about this issue some time ago for the `views` package, which has
a separation between sequences (seq) and other iterables (gen):
https://github.com/k7hoven/views
The functionality provided by `views.gen` is not that interesting—it's
essentially a subset of itertools functionality, but with an API that
parallels `views.seq` which works with sequences (iterable, sliceable,
chainable, etc.). I used the name `gen`, because iterator/iterable variants
of the functionality can be implemented with generator functions (although
also with other kinds of iterators/iterables). Calling the thing `iter`
would have conflicted with the builtin `iter`.
HOWEVER, this naming can be confusing for those that lean more towards
using "generator" to also mean "generator function", and for those that are
comfortable with the term "iterator" despite its resemblance to "iterable".
Now I'm actually seriously considering to consider renaming `views.gen` to `
views.iter` when I have time. After all, there's already `views.range`
which "conflicts" with the builtin range.
Anyway, the point is that the naming is suboptimal.
SOLUTION: Maybe (a) all iterators should be called iterators 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.
(I'm not going into the distinction between things that can receive values
via `send` or any other possible distinctions between different types of
iterators and iterables.)
—Koos
(discussion originated from python-ideas, but cross-posted to python-dev
in case there's more interest there)
--
+ Koos Zevenhoven + http://twitter.com/k7hoven +
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171127/17a1455e/attachment-0001.html>
More information about the Python-ideas
mailing list