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. 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
-- http://mail.python.org/mailman/listinfo/python-list