Method returning an Iterable Object

Anjanesh Lekshminarayanan mail at anjanesh.net
Tue Jan 27 04:16:38 CET 2009


But how come a raise StopIteration in the next() method doesnt need to
be caught ? It works without breaking.

class twoTimes:
    max = 10**10

    def __init__(self, n):
        self.__n = n

    def next(self):
        if self.__n > self.max:
            raise StopIteration
        self.__n *= 2
        return self.__n

    def __iter__(self):
        return self


t = twoTimes(5)
c = 0

print (t.next())
print (t.next())

for n in t:
    print n


Anjanesh



More information about the Python-list mailing list