[Tutor] Iterators and Generators...
Neil Schemenauer
nas-pytut at python.ca
Wed Aug 13 09:49:48 EDT 2003
Marc Barry wrote:
> I have a class which is an iterator, but I can't figure out why it is not
> working.
This is probably what you want:
class list_iterator:
def __init__(self, a_list):
self.a_list = a_list
def __iter__(self):
for i in self.a_list:
yield i
A function containing a 'yield' statement returns a generator when it is
called. Calling '.next()' on that generator object starts executation
of the function body until a yield statement is reached (and so on). If
the end of the function is reached then StopIteration is raised.
HTH,
Neil
More information about the Tutor
mailing list