How does this script work? #!/usr/bin/python
class IteratorExample: def __init__(self, s): self.s = s self.next = self._next().next self.exhausted = 0 def _next(self): if not self.exhausted: flag = 0 for x in self.s: if flag: flag = 0 yield x else: flag = 1 self.exhausted = 1 def __iter__(self): return self._next() def main(): a = IteratorExample('edcba') for x in a: print x print '=' * 30 a = IteratorExample('abcde') print a.next() print a.next() print a.next() print a.next() print a.next() print a.next() if __name__ == '__main__': main() Here is the output: d b ============================== b d Traceback (most recent call last): File "./python_101_iterator_class.py", line 35, in ? main() File "./python_101_iterator_class.py", line 29, in main print a.next() StopIteration I think a lot of my confusion comes from not understanding what _next is. I got this script from an online tutorial at python.org. Is there a better way to write the script, so I can actually understand it? "I'm the last person to pretend that I'm a radio. I'd rather go out and be a color television set." -David Bowie "Who dares wins" -British military motto "I generally know what I'm doing." -Buster Keaton _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor