Iterators, iterables and special objects

Peter Slížik peter.slizik at gmail.com
Thu Jul 23 03:54:12 EDT 2020


> The view are iterables.  They can be iterated more than once and used in
> other operations.
>
> The transformers should be once-through iterators because they can be
> passed once-through interators.


This is important, thank you for pointing it out.



> Python's design that iter(iterator) is iterator is extremely handy.
>

Yes,  but it has the unfortunate consequence that an iterator is expected
to define

def __iter__(self):    return self

which I saw people *not* doing, supposedly because the official
documentation says that it needs to be done, but does not explain why (and
the interpreter does not enforce it, and the code works anyway).

Moreover, some tutorial authors make it even more difficult with using the
terms iterator and iterable interchangeably. A notorious example is this
wiki:
https://wiki.python.org/moin/Iterator

It says:

*Here is an *iterator* that returns a random number of 1's: *

class RandomIterable:    def __iter__(self):        return self

I remember the time when I started to look into iterators, and frankly,
with this wiki article, it took me ages to understand,why do iterables
return self in their *__iter__* method.

But I think I'm getting off-topic...

Again, thanks guys, @Terry and @dn for your explanations, you've made the
situation clear.
Peter


More information about the Python-list mailing list