Stephen Hansen wrote:

On Fri, Oct 16, 2009 at 5:22 AM, Duncan Booth
<duncan.bo...@invalid.invalid>wrote:

Chris Rebert <c...@rebertia.com> 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.

HTH,
Harald.

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to