Unbound or infinite iterator


So far each iterator had a beginning and an end. However we can also create infinte or unbounded iterators. The nice thing about them is that we can pass them around as we do with any other object and we can execute operations on them without burning our CPU.

Of course the user will have to be carefull not to try to flatten the iterator, not to try to get all the values from it, as that will only create an infinite loop or a never ending operation.

In this very simple example we count from 0 and we never stop.

When we use the Counter in the for loop we need to include a stop-condition, otherwise our loop will never end.


examples/iterators/iterator.py
class Counter():
   def __init__(self):
       self.count = 0

   def __iter__(self):
       return self

   def __next__(self):
       self.count += 1
       return self.count

for c in Counter():
   print(c)
   if c > 10:
       break

1
2
3
4
5
6
7
8
9
10
11