class test:
def __init__(self,start,stop):
self.value=start-1
self.stop=stop
def __iter__(self):
return self
def __next__(self):
if self.value==self.stop:
raise StopIteration
self.value+=1
return self.value**2
for i in test(1,5):
print(i)
自定义迭代器
最新推荐文章于 2022-07-04 17:28:35 发布