Iterators

Chris Rebert clp2 at rebertia.com
Fri Oct 16 12:51:13 CEST 2009


On Fri, Oct 16, 2009 at 2:24 AM, Kelson Folkvard Braaten ZAWACK
<zawackkfb at gis.a-star.edu.sg> wrote:
> Recently I was iterating through both a list and a file and I noticed a
> difference in behavior.  When I create an iterator for a list by calling
> iter(list_name) and then call the iterators  next method I get the first
> element in the list.  When I then create another iterator over the same list
> and call that iterators next method I again get the first element of the
> list, as I expected.  When  I create 2 iterators on a file in the manner
> described above that second iterator returns the second line of the file.
> This seems inconsistent.  Am I doing something wrong?  Is there a reason for
> this?

Essentially, file iterators are dumb and don't keep track of where in
the file the next line starts, instead relying on their associated
file object to keep track of the current position in the file; the
iterator's state is little more than a reference to its associated
file object. When asked for the "next" line, a file iterator just
reads forward to the next newline from the file object's current
position, changing the current position as tracked by the file object
as a side-effect. Thus, using multiple iterators to the same file
object can have the results you're seeing when these side-effects
interact.

List iterators are, in contrast, "smart" and each independently store
the index of the next item to yield as part of their state. Thus,
using multiple iterators to the same list works more intuitively
(provided the list isn't mutated during iteration, but that caveat
applies in the single-iterator case anyway).

I would guess that the file iterators' relative "stupidity" is likely
for efficiency and simplicity reasons.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list