Re: Use-cases for alternative iterator

2011-03-11 Thread Terry Reedy
On 3/11/2011 1:43 AM, Steven D'Aprano wrote: The iter() built-in takes two different forms, the familiar iter(iterable) we all know and love, and an alternative form: iter(callable, sentinel) E.g.: T = -1 def func(): ... global T ... T += 1 ... return T ... it = iter(func, 3)

Re: Use-cases for alternative iterator

2011-03-11 Thread Peter Otten
Steven D'Aprano wrote: > The iter() built-in takes two different forms, the familiar > iter(iterable) we all know and love, and an alternative form: > > iter(callable, sentinel) > I've never seen this second form in actual code. Does anyone use it, and > if so, what use-cases do you have? I fo

Re: Use-cases for alternative iterator

2011-03-10 Thread Hrvoje Niksic
Steven D'Aprano writes: > I've never seen this second form in actual code. Does anyone use it, > and if so, what use-cases do you have? Since APIs that signal end-of-iteration by returning a sentinel have fallen out of favor in Python (with good reason), this form is rare, but still it's sometim

Use-cases for alternative iterator

2011-03-10 Thread Steven D'Aprano
The iter() built-in takes two different forms, the familiar iter(iterable) we all know and love, and an alternative form: iter(callable, sentinel) E.g.: >>> T = -1 >>> def func(): ... global T ... T += 1 ... return T ... >>> it = iter(func, 3) >>> next(it) 0 >>> next(it) 1 >>> next(