迭代器模式,不一次性直接返回结果,调用一次方法,返回一个。
class Iterator(object):
def __init__(self, iterator_list):
super().__init__()
self.list = iterator_list
self.current_index = 0
self.max_index = 0
def __iter__(self):
self.current_index = 0
self.max_index = len(self.list) - 1
return self
def __next__(self):
if self.current_index <= self.max_index:
one = self.list[self.current_index]
self.current_index += 1
return one
else:
raise StopIteration
def main():
iterator = Iterator([5, 9, 7, 'x', 'e'])
for one in iterator:
print(one)
if __name__ == '__main__':
main()