[Python-ideas] Introduce collections.Reiterable

Paul Moore p.f.moore at gmail.com
Fri Sep 20 12:03:17 CEST 2013


On 20 September 2013 10:48, Steven D'Aprano <steve at pearwood.info> wrote:
> Actually, I think the collections ABC gets it wrong, according to both
> common practice and the definition given in the glossary:
>
> http://docs.python.org/3.4/glossary.html
>
> More on this below.
>
> As for my comment above, dict views don't obey the iterator protocol
> themselves, as they have no __next__ method, nor do they obey the
> sequence protocol, as they are not indexable. Hence they are not
> *directly* iterable, but they are *indirectly* iterable, since they have
> an __iter__ method which returns an iterator.
>
> I don't think this is a critical distinction. I think it is fine to call
> views "iterable", since they can be iterated over. On the rare occasion
> that it matters, we can just do what I did above, and talk about objects
> which are directly iterable (e.g. iterators, sequences, generator
> objects) and those which are indirectly iterable (e.g. dict views).

An iterable is an object that returns an iterator when passed to
iter(). It's *iterators* that have to have __next__, not iterables. An
iterable hast to have __iter__, which as far as I know dict views do

>>> {}.keys().__iter__
<method-wrapper '__iter__' of dict_keys object at 0x00000000027366D8>

Paul


More information about the Python-ideas mailing list