#迭代器使用
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)
python迭代器简单操作
最新推荐文章于 2023-02-07 16:50:48 发布