Exception handling in Python 3.x

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Dec 6 19:23:06 EST 2010


On Mon, 06 Dec 2010 13:13:40 -0800, Paul Rubin wrote:

> It's really unfortunate, though, that Python 3 didn't offer a way to
> peek at the next element of an iterable and test emptiness directly.

This idea of peekable iterables just won't die, despite the obvious flaws 
in the idea.

There's no general way of telling whether or not a lazy sequence is done 
except to actually generate the next value, and caching that value is not 
appropriate for all such sequences since it could depend on factors which 
have changed between the call to peek and the call to next.

If you want to implement a peek method in your own iterables, go right 
ahead. But you can't make arbitrary iterables peekable without making a 
significant class of them buggy.



-- 
Steven



More information about the Python-list mailing list