(continued from previous pagenext(it'anext(it'bnext(it'cnext(ittraceback (most recent call last)file ""line in next(itstopiteration having seen the mechanics behind the iterator protocolit is easy to add iterator behavior to your classes define an __iter__(method which returns an object with __next__(method if the class defines __next__()then __iter__(can just return selfclass reverse"""iterator for looping over sequence backwards ""def __init__(selfdata)self data data self index len(datadef __iter__(self)return self def __next__(self)if self index = raise stopiteration self index self index return self data[self indexrev reverse('spam'iter(revfor char in revprint(charm generators generators are 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 itthe generator resumes where it left off (it remembers all the data values and which statement was last executedan example shows that generators can be trivially easy to createdef reverse(data)for index in range(len(data)- - - )yield data[index classes