1"""
2Example for a Class that creates an iterator for each word in a sentence
3
4The keyword 'yield' is key (no pun intended) to the solution. It works the
5same as return with the exception that on the next call to the function it
6will resume where it left off
7"""
8
9class Counter:
10 def __init__(self, low, high):
11 self.current = low - 1
12 self.high = high
13
14 def __iter__(self):
15 return self
16
17 def __next__(self): # Python 2: def next(self)
18 self.current += 1
19 if self.current < self.high:
20 return self.current
21 raise StopIteration
22
23
24for c in Counter(3, 9):
25 print(c)