An iterator with look-ahead
Fredrik Lundh
fredrik at pythonware.com
Wed Jan 10 09:48:16 EST 2007
Neil Cerutti wrote:
> For use in a hand-coded parser I wrote the following simple
> iterator with look-ahead. I haven't thought too deeply about what
> peek ought to return when the iterator is exhausted. Suggestions
> are respectfully requested. As it is, you can't be sure what a
> peek() => None signifies until the next iteration unless you
> don't expect None in your sequence.
if you're doing simple parsing on an iterable, it's easier and more efficient
to pass around the current token and the iterator's next method:
http://online.effbot.org/2005_11_01_archive.htm#simple-parser-1
</F>
More information about the Python-list
mailing list