Consider the following minimum working example:
import itertools
for i in iter(itertools.count, 10):
print(i)
I have expected that the output counts to 10. However, the output was count(0)
over and over again. Printing the type instead gives <class 'itertools.count'>
.
The documentation of iter(object, sentinel)
says the following:
The iterator created in this case will call object with no arguments for each call to its
__next__()
method; if the value returned is equal to sentinel,StopIteration
will be raised, otherwise the value will be returned.
Which reads to me like the behaviour I have expected. What have I overlooked? Optional bonus question: How is it possible with iter
to make object a generator and get the expected behaviour?