from collections import Iterable class MyIter: def __init__(self,items): self.iter1=items self.cur1=0 def __iter__(self): return self def __next__(self): if self.cur1<len(self.iter1): self.cur1+=1 return self.iter1[self.cur1-1] else: raise StopIteration class MyList(): def __init__(self): self.items=list() def append_items(self,items): self.items.append(items) def __iter__(self): myiter = MyIter(self.items) return myiter mylist=MyList() mylist.append_items(2) print(isinstance(mylist,Iterable)) for i in mylist: print(i)
创建迭代器
最新推荐文章于 2022-11-05 10:22:28 发布