[Python-ideas] Introduce collections.Reiterable
Andrew Barnert
abarnert at yahoo.com
Sat Sep 21 06:18:29 CEST 2013
On Sep 20, 2013, at 14:15, Terry Reedy <tjreedy at udel.edu> wrote:
>> False
>> py> list(s) # definitely iterable
>> [1000, 1001, 1002, 1003, 1004]
>
> I tested and iter() recognizes Seqs as iterables:
>
> for i in iter(Seq()): print(i)
> <same numbers as above>
>
> It does, however, wrap them in an adaptor iterator class
What else did you expect? Sequences are iterables, but they aren't iterators. So calling iter on one can't return the sequence itself.
>>>> type(iter(Seq()))
> <class 'iterator'>
> (which I was not really aware of before ;-) with proper __iter__ and __next__ methods
>>>> si is iter(si)
> True
>>>> next(si)
> 1000
Having an __iter__ that returns itself and a __next__ is the definition of what an iterator is. And returning an iterator is the whole point of the iter function. So what else could it do in this case?
Think about how you'd implement iter in pure python. You'd try to return its __iter__(), and on AttributeError, you'd return a generator. So the C implementation does the same thing, but, as usual, substitutes a custom C iterator for a generator.
More information about the Python-ideas
mailing list