Python对象实现迭代器协议
在类当中必须实现 __iter__ 方法,
在类当中必实实现 __next__方法,
class Foo: def __init__(self): self.x=0 def __iter__(self): return self def __next__(self): self.x+=1 if self.x== 10: raise StopIteration return self.x obj=Foo() obj.__next__() """ 类必须实现 __iter__ 和__next__ 方法后,对象才可以实现跌代 """ for i in obj: print(i)