I am trying to create a generator that will return the natural numbers in order. This is used to enumerate another generator which will exit upon StopIteration
, which seems like the easiest way to do it. However, I cannot find an idiomatic way of creating this generator:
def numbers():
i = 0
while True:
yield i
i += 1
q = Queue.Queue()
for i in numbers():
try:
q.put((i, my_generator.next()))
except StopIteration:
break
This does work, but it seems unpythonic to use a while True:
in this way.
Is there a standard library function to iterate over the natural numbers?