python 遍历器

# conding : utf-8
# 遍历器

class BookCollection:
def __init__(self):
self.data = ['《往事》','《只能》','《回味》']
self.cur = 0

def __iter__(self):
return self

def __next__(self):
if self.cur >= len(self.data):
raise StopIteration
r = self.data[self.cur]
self.cur += 1
return r
books = BookCollection()
for book in books:
print(book)
posted @ 2018-08-01 16:24  白水煮茶  阅读(479)  评论(0编辑  收藏  举报