[Python-ideas] Introduce collections.Reiterable

Terry Reedy tjreedy at udel.edu
Sat Sep 21 01:48:48 CEST 2013


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.

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list