[Python-ideas] Introduce collections.Reiterable

Neil Girdhar mistersheik at gmail.com
Tue Sep 24 02:19:40 CEST 2013

If infinite sequences are so common, it might be better to add a
collections.abc for them.  Then, besides a default __iter__ being
generated, you could automatically generate advanced slicing into the
original infinite sequence that returns a Sequence.   This slice would then
support the automatically-generated .index method, etc.

Also, the big advantage to inheriting from an abc rather than defining
__getitem__and counting on Python silently allowing __iter__ to work is
that the former is an explicit declaration of intent.  The latter counts on
people knowing a weird feature of python.



On Mon, Sep 23, 2013 at 10:23 AM, Steven D'Aprano <steve at pearwood.info>wrote:

> 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
> "Reiterable".
> --
> Steven
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> --
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "python-ideas" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/python-ideas/OumiLGDwRWA/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> python-ideas+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130923/0370f1b6/attachment-0001.html>

More information about the Python-ideas mailing list