Harald Kraemer harryk at
Fri Oct 16 18:24:13 CEST 2009

Stephen Hansen wrote:

> On Fri, Oct 16, 2009 at 5:22 AM, Duncan Booth
> <duncan.booth at invalid.invalid>wrote:
>> Chris Rebert <clp2 at> wrote:
>> > Essentially, file iterators are dumb and don't keep track of where in
>> > the file the next line starts,
> [snip]
>> 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
> Except it is, IMHO, dumb vs smart-- but that doesn't mean the person who
> implemented it is dumb nor that its dumb to have such an implementation.

I think, a better terminology would be iterable versus iterator. An 
iterable object can return an iterator (which is then used to iterate 
over the elements of the iterable), while the iterator is just ... an 
iterator :)

Given this, it makes sense again: a file-iterator f is an iterator and 
keeps returning itself upon calling iter(f) for convenience, while a 
list l is an iterable, and iter(l) will return a new list-iterator 
again. This especially makes sense if the construction of an iterator is 
expensive (compare the file iterator, as it 'costs' either memory (to 
store the whole file), or a file handle).

A final remark would be that you can turn any iterator into an iterable, 
by calling list(iterator) and explicitly 'pay' memory in order to get an 
iterable from the iterator.


More information about the Python-list mailing list