PHP
·
发表于 5年以前
·
阅读量:8410
>>> class TestIter():
def __init__(self,lst):
self.lst = lst
# 重写可迭代协议__iter__
def __iter__(self):
print('__iter__ is called')
return iter(self.lst)
# =========================
# 迭代 TestIter 类:
# =========================
>>> t = TestIter()
>>> t = TestIter([1,3,5,7,9])
>>> for e in t:
print(e)
__iter__ is called
1
3
5
7
9