[Python-ideas] Introduce collections.Reiterable

Andrew Barnert abarnert at yahoo.com
Fri Sep 20 18:21:43 CEST 2013

On Sep 20, 2013, at 5:45, Steven D'Aprano <steve at pearwood.info> wrote:

> On Fri, Sep 20, 2013 at 06:18:47AM -0400, Neil Girdhar wrote:
>> On Fri, Sep 20, 2013 at 5:48 AM, Steven D'Aprano <steve at pearwood.info>wrote:
>>> On Thu, Sep 19, 2013 at 11:02:57PM +1000, Nick Coghlan wrote:
>>>> On 19 September 2013 22:18, Steven D'Aprano <steve at pearwood.info> wrote:
>>> [...]
>>>>> At the moment, dict views aren't directly iterable (you can't call
>>>>> next() on them). But in principle they could have been designed as
>>>>> re-iterable iterators.
>>>> That's not what iterable means. The iterable/iterator distinction is
>>>> well defined and reflected in the collections ABCs:
>>> 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
>> Where does the glossary disagree with collections.abc?
> I show below a class that is iterable, yet is not an instance of 
> collections.Iterable. By the glossary definition it is iterable (it has 
> a __getitem__ method that raises IndexError when there are no more items 
> to be returned).
> [...]
>> What you're calling "indirectly iterable" is what the docs call "Iterable"
>> and what collections.abc call Iterable, right?
> I've explained this further in my reply to Paul Moore. What I should 
> have said was *manually* iterable, in the sense of directly calling 
> __next__ or __getitem__ on the view.

Being able to call __next__ on something is not a property of an iterable. It's only a property of an iterator. (In fact, I think python could have defined an iterator as "an iterable with __next__" just as profitably as "an iterable whose __iter__() returns itself", and ended up with the exact same categories as today. But that's not important.)

So, I'm not sure what your "manually iterable" is supposed to represent. Iterators and sequences but not other iterables? What does this distinction buy you? It seems as useful as inventing a word for all all wedge-headed cats plus tabby apple-headed cats but no other apple-headed cats: a perfectly definable category, but one of no value.

And that makes me think you're still confusing iterables and iterators. 

Except that you've pointed out a valid distinction--making something indexable (by an initial sequence of natural numbers? or does a 1-based array or an otherwise-not-iterable mapping-like object count as an empty iterator?) makes it work with the iterable protocol, but not the Iterable ABC, so clearly you know what you're talking about.

And that makes me think that I (and the people who have been responding to you before me) have missed something important in this "manually iterable" or "directly iterable" idea. So, maybe you should try explaining it a different way?

More information about the Python-ideas mailing list