Iterator / Iteratable confusion

Francis Girard francis.girard at free.fr
Mon Feb 14 15:31:50 EST 2005


Le dimanche 13 Février 2005 23:58, Terry Reedy a écrit :
> Iterators are a subgroup of iterables.  Being able to say iter(it) without
> having to worry about whether 'it' is just an iterable or already an
> iterator is one of the nice features of the new iteration design.
>
> Terry J. Reedy

Hi,

I have difficulties to represent an iterator as a subspecie of an iteratable 
as they seem profoundly different to me. But it just might be that my mind is 
too strongly influenced by the C++ STL where there is a clear (I resist to 
say "clean") distinction between iteratable and iterator.

One of the result of not distinguishing them is that, at some point in your 
programming, you are not sure anymore if you have an iterator or an 
iteratable ; and you might very well end up calling "iter()" or "__iter__()" 
everywhere.

I am not concerned with the small performance issue involved here (as I very 
seldom am) but with clarity. After all, why should you have to call __iter__ 
on an iterator you just constructed (as in my dummy "example 2") ? One might 
wonder, "what ? isn't this already the iterator ?" But this, I agree, might 
very well just be a beginner (as I am) question, trying to learn the new 
iterator semantics.

I have a strong feeling that the problem arises from the difficulty to marry 
the familiar ""for ... in ..."" construct with iterators. If you put an 
iteratable in the second argument place of the construct (as is traditionally 
done) then the syntax construct itself is an implicit iterator. Now, if you 
have explicit iterators then they don't fit well with the implicit iterator 
hidden in the syntax.

To have iterators act as iteratables might very well had been a compromise to 
solve the problem.

I am not sure at all that this is a "nice feature" to consider an iterator at 
the same level that an iteratable. It makes it a bit more akward to have the 
"mind impulse", so to speak, to build iterators on top of other iterators to 
slightly modify the way iteration is done. But I think I'm getting a bit too 
severe here as I think that the compomise choice made by Python is very 
acceptable.

Regards,

PS : I am carefully reading Micheal Spencer very interesting reply.

Thank you,

Francis Girard








More information about the Python-list mailing list