python迭代器简单操作

#迭代器使用
class A(object):
    def __init__(self, list):
        self.list = list
        self.i = 0

    def __iter__(self):
        return self

    def __next__(self):
        self.i += 1
        print("i:%d,list读到第:%d" % (self.i, len(self.list)))
        if self.i == len(self.list):
            raise StopIteration
        return self.list[self.i]


a = [1, 2, 3, 4, 5]
for temp in A(a):
    print(temp)

猜你喜欢

转载自blog.csdn.net/weixin_42670402/article/details/82313175