Iterators : iter « Class « Python Tutorial






class FileList:
    def __init__(self):
        self.ptr = 0
        self.file_list = ["file1", "file2", "file3", "file4" ]
    def __iter__(self):
        self.ptr = 0
        return self
    def next(self):
        if self.ptr == len(self.file_list):
           raise StopIteration
        s = self.file_list[self.ptr]
        self.ptr = self.ptr + 1
        return s

fl = FileList()
for f in fl :
    print f








11.21.iter
11.21.1.Iterators
11.21.2.Make a new iterator object for each new iteration
11.21.3.User-Defined Iterators