[Oren Tirosh]
class ifile(file): def __iter__(self): return self
def next(self): s = self.readline() if s: return s raise StopIteration
class xfile: def __init__(self, filename): self.filename = filename
def __iter__(self): return ifile(self.filename)
This pair of objects has a proper container/iterator relationship.
This is all clear to me, except for one little thing. I wonder why class `ifile' has an `__iter__' method itself. I know it is said to be the "iterator protocol", and I wonder why it has to be. My understanding is that `__iter__' returns an iterator all ready to be enquired a number of times through `.next()' calls, and I presume that if any re-initialisation has to take place, it is within `__iter__'. However, as the iterator maintains its own progressive state, I do not see the intent and purpose of the iterator having an `__iter__' method itself. Would it make sense using the iterator `__iter__' as the preferred place where it re-initialises itself? -- François Pinard http://www.iro.umontreal.ca/~pinard