[Python-ideas] Introduce collections.Reiterable

Stephen J. Turnbull stephen at xemacs.org
Sat Sep 21 04:41:10 CEST 2013

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).

More information about the Python-ideas mailing list