Iterators

Simon Forman sajmikins at gmail.com
Sat Oct 17 04:00:52 CEST 2009


On Fri, Oct 16, 2009 at 8:22 AM, Duncan Booth
<duncan.booth at invalid.invalid> wrote:
> Chris Rebert <clp2 at rebertia.com> wrote:
>
>> 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.
>
> Nothing 'dumb' or 'smart' about it: it is simply that a file object is
> already an iterator. Trying to create an iterator from an existing iterator
> in Python never duplicates the iterator.
>
>>>> f = open('somefile')
>>>> iter(f) is f
> True
>

The OP's question has been answered but since no one mentioned the
itertools.tee() function yet I figured I would..

http://docs.python.org/library/itertools.html#itertools.tee


In [1]: open('afile', 'w').write('''line one
   ...: line two
   ...: line three''')

In [2]: from itertools import tee

In [3]: i0, i1 = tee(open('afile'))

In [4]: i0.next()
Out[4]: 'line one\n'

In [5]: i0.next()
Out[5]: 'line two\n'

In [6]: i1.next()
Out[6]: 'line one\n'

In [7]: i0.next()
Out[7]: 'line three'



More information about the Python-list mailing list