On Tue, Jan 27, 2009 at 1:16 PM, Anjanesh Lekshminarayanan <m...@anjanesh.net> wrote: > But how come a raise StopIteration in the next() method doesnt need to > be caught ? It works without breaking.
Because this exception is specially dealt with when iterating over an iterator. The "raise StopIteration" is what causes the iteration to stop. Consider: >>> x = EveryOther(xrange(10)) >>> x.next() 0 >>> x.next() 2 >>> x.next() 4 >>> x.next() 6 >>> x.next() 8 >>> x.next() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "foo.py", line 12, in next raise StopIteration StopIteration >>> x = EveryOther(xrange(10)) >>> list(x) [0, 2, 4, 6, 8] >>> cheers James -- http://mail.python.org/mailman/listinfo/python-list