python的iterator object,pythoniterator,class Square


class Squares:    def __init__(self, start, stop):        self.value = start - 1        self.stop  = stop    def __iter__(self):                   # get iterator object        return self    def next(self):                       # on each for iteration        if self.value == self.stop:            raise StopIteration        self.value += 1        return self.value ** 2for i in Squares(1,5):     print i,X = Squares(1,5)X = Squares(1,5)print [n for n in X]                     # exhausts itemsprint [n for n in X]                     # now it's emptyprint [n for n in Squares(1,5)]print list(Squares(1,3))

评论关闭