Search the web
Sign In
New User? Sign Up
python-iter
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Message search is now enhanced, find messages faster. Take it for a spin.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Re: [Python-iterators] Re: [Python-Dev] Shall I start adding iterat   Message List  
Reply | Forward Message #204 of 207 |
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 ?

--
Thomas Wouters <thomas@...>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!



Fri Apr 20, 2001 9:26 am

thomas@...
Send Email Send Email

Forward
Message #204 of 207 |
Expand Messages Author Sort by Date

... 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...
Thomas Wouters
thomas@...
Send Email
Apr 20, 2001
9:27 am

I've redirected replies to python-iterators@.... The archives work now: http://www.geocrawler.com/lists/3/SourceForge/9283/0/ --Guido van...
Guido van Rossum
guido@...
Send Email
Apr 20, 2001
2:39 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help