class _Iterator: """ 迭代器类 """ def __init__(self,seq=None): self._bag_items = [1,2,3,4,5] if seq is None else seq self._cur_item = 0 def __iter__(self): return self def __next__(self): if self._cur_item < len(self._bag_items): item = self._bag_items[self._cur_item] self._cur_item += 1 return item else: raise StopIteration
__next__ for循环每次迭代会调用
__iter__ 返回本身就行, 本身就是迭代器