[Python-ideas] Introduce collections.Reiterable

Neil Girdhar mistersheik at gmail.com
Sat Sep 21 06:20:39 CEST 2013


I can humbly suggest why Python would deprecate the sequence protocol:
there "should be one obvious way" to answer iter(), and in my opinion
that's the  __iter__()  method.  I considered infinite iterators, and if
you happen to have  __getitem__ written, you can trivially write an
__iter__ function as follows:

def __iter__(self):
     return (self.__getitem__(x) for x in itertools.count())

Now your class will be Iterable in the abc sense, and no longer relies on
the sequence protocol

Best,
Neil


On Fri, Sep 20, 2013 at 10:41 PM, Stephen J. Turnbull <stephen at xemacs.org>wrote:

> Tim Delaney writes:
>
>  > Also, pathological is probably not the best term to use. Instead,
>  > substitute "deliberately breaks a well-established protocol".
>
> Note that in Neil's use case (the OP) it's not deliberate.  His
> function receives an iterable, it naively iterates it and (if an
> iterator) consumes it, and then some other function loses.  Silently.
>
> Also, as long as __getitem__(0) succeeds, this *is* the "sequence
> protocol".  (A Sequence also has a __len__() method, but iterability
> doesn't depend on that.)
>
> I don't see why Python would deprecate this.  For example, consider
> the sequence of factors of integers: [(1,2), (1,3), (1,2,2,4), (1,5),
> (1,2,3,6), ...].  Factorization being in general a fairly expensive
> operation, you might want to define this in terms of __getitem__() but
> __len__() is infinite.  I admit this is a somewhat artificial example
> (I don't know of non-academic applications for this sequence, although
> factorization itself is very useful in applications like crypto).
> _______________________________________________
> 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/20130921/0bc07c3e/attachment.html>


More information about the Python-ideas mailing list