[Python-iterators] Re: [Python-Dev] Shall I start adding iterators to Python 2.2?

M.-A. Lemburg mal@lemburg.com
Fri, 20 Apr 2001 12:10:10 +0200


Thomas Wouters wrote:
> 
> On Fri, Apr 20, 2001 at 11:02:09AM +0200, M.-A. Lemburg wrote:
> 
> > > - There's an operation to create an iterator from a function and a
> > >   sentinel value.  This is spelled as iter(function, sentinel).  For
> > >   example,
> > >
> > >     for line in iter(sys.stdin.readline, ""):
> > >       ...
> > >
> > >   is an efficient loop over the lines of stdin.
> 
> > Hmm, I guess you have to compare each function output to the
> > sentinel then, right ? This can be very expensive.
> 
> > Wouldn't an exception base class also do the trick as sentinel ? The
> > iterator would then stop when an exception is raised by the
> > function which matches the sentinel exception.
> 
> The sentinel method is for use with existing functions, that return a
> sentinel value (like "" or None or whatever.) Comparing to those is not
> terribly expensive, asside from the burden of running a single compare in
> the inner loop. Rewriting those functions to raise an exception instead
> would be, well, somewhat silly -- if you're rewriting them anyway, why not
> just make an iterator out of them ?

I wasn't clear enough: I was proposing to also allow exception
classes as sentinel which are then not compared with the result
of the function, but instead with any exceptions raised by the
function. This would be an additional method of recognizing the
end-of-iteration to the standard return value comparison.

The reasoning is the you may want to use e.g. a C function (hard
to rewrite as iterator) as iterator which currently works along 
these lines:

while 1:
    try:
        x = datasource()
    except EndOfData:
        break
    print x

You could then rewrite this as:

for x in iter(datasource, EndOfData):
   print x

BTW, how does iter() recognize that it is supposed to look
for a sentinel ?

-- 
Marc-Andre Lemburg
______________________________________________________________________
Company & Consulting:                           http://www.egenix.com/
Python Pages:                           http://www.lemburg.com/python/