[Python-ideas] Introduce collections.Reiterable

Tim Delaney timothy.c.delaney at gmail.com
Sat Sep 21 02:05:35 CEST 2013


On 21 September 2013 09:48, Terry Reedy <tjreedy at udel.edu> wrote:

> On 9/20/2013 6:00 PM, Tim Delaney wrote:
>
>  I think there is a distinction here between collections.Iterable (as a
>> defined ABC) and something that is "iterable" (lowercase "i"). As you've
>> noted, an "iterable" is "An object capable of returning its members one
>> at a time".
>>
>> So I think a valid definition of reiterable (barring pathological cases)
>> is:
>>
>>      obj is not iter(obj)
>>
>
> If obj has a fake __getitem__, that will not work.
>
> class Cnt:
>     def __init__(self, maxn):
>         self.n = 0
>         self.maxn = maxn
>     def __getitem__(self, dummy):
>         n = self.n + 1
>         if n <= self.maxn:
>             self.n = n
>             return n
>         else:
>             raise IndexError
>
> c3 = Cnt(3)
> print(c3 is not iter(c3), list(c3), list(c3))
> >>>
> True [1, 2, 3] []
>
> Dismissing legal code as 'pathological', as more than one person has, does
> not cut it as a design principle.


To me, that is a reiterable. It might not give the same results each time
through, but you can iterate, it stops, then you can iterate over it again
- it won't raise an exception trying to do so. So not what I would consider
a pathological case - though definitely an unusual case and one that
obviously wouldn't work in many situations that require reiterables to
return the same values in the same order each time through.

So we've got two classes of reiterables here

- anything that can be iterated through, and then iterated through again,
for which obj is not iter(obj) will work in all but what I consider to be
pathological cases;

- iterables that can be iterated through multiple times, returning the same
objects in the same order each time through, for which I don't think a test
is possible.

Tim Delaney
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130921/a101508f/attachment.html>


More information about the Python-ideas mailing list