Iterators

Stephen Hansen apt.shansen at gmail.com
Fri Oct 16 11:45:29 EDT 2009


On Fri, Oct 16, 2009 at 5: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,
>
[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.

One way of implementing an iterator is to have __iter__ simply return self,
and implement it directly on the object via a next method. But that's the
dumb way-- or perhaps simple is nicer to say :) In that, the iterator is
"dumb" because it only keeps track of 'where am I' for the next() once for
the entire object... basically in the file's .tell() position.

A list iterator is smart in that it keeps track of where it is
independently.

I'm not objecting to file iterators being that way-- my usage of dumb isn't
to say that its dumb for them to do it. On the contrary, I have no idea how
a smart iterator could be implemented for files consistently at all, short
of having each iterator duplicate the file handle... which is a whoooole
level of complication I don't really think files should go about doing :)

I wish docs for software / libraries would mark their iterables as either
Smart or Dumb, based on if they implement the protocol directly or if they
return a new iterator object on every call. It seems an easy way to clearly
articulate the difference in approaches, but I suppose some people would
take dumb to mean something else. :)

--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091016/78333fec/attachment.html>


More information about the Python-list mailing list