def __iter__ (self): return self def __next__ (self): if self.index ==0: raise StopIteration self.index =self.index -1 return self.data[self.index] 

>>> rev =Reverse('spam') >>> iter(rev) <__main__.Reverse object at 0x00A1DB50> >>> for char in rev: ... print(char) ... m a p s 

## 9.9 Generators 

(continued from previous page) 

Generators are a simple and powerful tool for creating iterators. They are written like regular functions but use the $_{yield}$statement whenever they want to return data. Each time$_{next()}$ is called on it, the generator resumes where it left off (it remembers all the data values and which statement was last executed). An example shows that generators can be trivially easy to create: 

def reverse(data): for index in range(len(data) -1,-1,-1): yield data[index] 

>>> for char in reverse('golf'): ... print(char) ... f l o g 

Anything that can be done with generators can also be done with class-based iterators as described in the previous section. What makes generators so compact is that the __iter__() and __next__() methods are created automatically. 

Another key feature is that the local variables and execution state are automatically saved between calls. This made the function easier to write and much more clear than an approach using instance variables like$_{self.index}$ and self.data . 

In addition to automatic method creation and saving program state, when generators terminate, they automatically raise$_{StopIteration}$. In combination, these features make it easy to create iterators with no more effort than writing a regular function. 

