迭代器
自定义迭代器:
class countDown:
def __init__(self,step): #实例化
self.step = step
def __next__(self): #返回下个元素
if self.step<=0:
raise StopIteration
self.step -= 1
return self.step
def __iter__(self): #返回迭代器实例
return self
for element in countDown(4):
print(element)
输出实例:
3
2
1
0