Python program to build a simple Iterator class
Learn to build a simple Iterator class in Python 3

Pre-requisites
How do iterators work?
Iterator objects in Python programming confirm to the iterator protocol. They provide two methods: __iter__()
and __next__()
to iterate over all the elements of the iterator.
__iter__
is called at the start of a loop. It returns the iterator object.__next__
method returns the next value in the iterator and is implicitly called at each loop increment. When there are no more value to return, it raises aStopIteration
example.
For more information on Python iterators, check out the pre-requisite article mentioned above.
Let’s build a simple Counter class to understand the concept.
Code
class Counter:
def __init__(self, low, high):
self.current = low - 1
self.high = high
def __iter__(self):
return self
def __next__(self):
self.current += 1
if self.current < self.high:
return self.current
raise StopIteration
for elem in Counter(1, 10):
print(elem)
Output
1
2
3
4
5
6
7
8
9
The above is a basic iterator class built with Python Object Oriented Programming.
This is where generators come to rescue.
Code
def counter(low, high):
current = low
while current < high:
yield current
current += 1
for elem in Counter(1, 10):
print(elem)
Output
1
2
3
4
5
6
7
8
9
As you can see, the printed output will be the same.