Use-cases for alternative iterator
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Fri Mar 11 01:43:58 EST 2011
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(it)
2
>>> next(it)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
I've never seen this second form in actual code. Does anyone use it, and
if so, what use-cases do you have?
--
Steven
More information about the Python-list
mailing list