[Python-ideas] Introduce collections.Reiterable

Stephen J. Turnbull stephen at xemacs.org
Tue Sep 24 05:42:18 CEST 2013

Steven D'Aprano writes:

 > A lot of work for virtually no benefit. Besides, who said that infinite 
 > iterators are common? 

Infinite, no.  Don't know the length until you're done, common.
Length nondeterministic and in principle unbounded, common.

 > If you care about infinite iterators, you can add your own "isinfinite" 
 > flag on them. Personally, I wouldn't bother. I just consider this a case 
 > for programming by contract: unless the function you are calling 
 > promises to be safe with infinite iterators, you should not use them.

But finite iterators can cause problems too (eg, Nick's length=1google
range -- even with an attosecond processor, that will take a while to
exhaust :-).  It would be nice if a program could choose its own value
of "too big", and process "large finite" and "infinite" lists in the
same way by taking "as much as possible".

That's what frustrates the OP -- it's *hard* to write a function that
makes a valid promise to be safe with all iterables.  (Of course his
definition of "safe" is much stricter, he requires "reiterable", not
just "finite and of 'reasonable' size".  But the principle is the same
-- Python should make it easy to write safe functions.  Of course Nick
is right: "Although practicality beats purity.")

More information about the Python-ideas mailing list