[Python-ideas] Introduce collections.Reiterable

Steven D'Aprano steve at pearwood.info
Mon Sep 23 16:23:37 CEST 2013

On Mon, Sep 23, 2013 at 05:04:10PM +0900, Stephen J. Turnbull wrote:
> Executive summary:
> The ability to create a quick iterable with just a simple __getitem__
> is cool and not a "hack" (ie, no need whatsoever to deprecate it), but
> it is clearly a "consenting adults" construction (which includes
> "knowing where your children are at 10pm").

When I first raised the issue that some iterables are not recognised by 
collections.Iterable as iterable, I asked to be convinced that it is not 
a bug. Now I'm convinced.

1) Objects which inherit from the Iterable ABC are not merely iterables 
in the sense of "can be iterated over", but also iterables in the ABC 
sense. Obviously. These can be considered "official" iterables, or 
perhaps Iterables with a capital I.

2) Objects with a __getitem__ method that obey the sequence protocol are 
also iterable in the sense of "can be iterated over", but if they don't 
inherit from Iterable they don't pass the ABC isinstance test.

Since "objects with a __getitem__ method that can be iterated over but 
that don't inherit from collections.Iterable" is a bit of a mouthful, 
for brevity I'm going to call them "de facto iterables", at the risk of 
being told off for inventing my own terminology *wink*

3) While it may appear strange to have something that can be iterated 
over not be recognised as an iterable, this is not very different from 
what can happen with duck-typing in general. We might write a class that 
duck-types as (say) a string, and have it not be recognised as such by 
isinstance(obj, string). Such is life.

4) If you want your de facto iterable to pass isinstance(obj, Iterable) 
tests, then you have to register it to make it official.

> This discussion is relevant because these are the kinds of things that
> bothered the OP.

Yes, we've certainly covered a lot of ground from the question of 


More information about the Python-ideas mailing list