array next pointer

Duncan Booth duncan.booth at invalid.invalid
Wed Mar 18 04:49:32 EDT 2009


Armin <feng.shaun at gmail.com> wrote:

> Could you give an example of next() with a sentinel and describe its
> use case please?  I have a little trouble understanding what you guys
> mean! 

It means you don't have to worry about next() throwing StopIteration. 

e.g.
>>> def pairs(sequence, padding=None):
	sequence = iter(sequence)
	for a in sequence:
		b = next(sequence, padding)
		yield a, b

		
>>> list(pairs('abcd'))
[('a', 'b'), ('c', 'd')]
>>> list(pairs('abcde'))
[('a', 'b'), ('c', 'd'), ('e', None)]


-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list