python自定义迭代器 python自定义迭代器的生成迭代器什么是迭代器12345iter(可迭代对象 )可以返回这个对象的的 迭代器实现了__iter__和__next__方法的对象都是迭代器对象迭代器对象是一个带状态的对象 __index记录当前迭代的位置迭代器也是一个可迭代对象,而可迭代对象不一定是迭代器 123456789101112131415#自定义实现迭代器class Stack: def __init__(self,*args): slef.__items = list(args) self.__index = 0 def __iter__(self): return self def __next__(self): if self.__index < len(self.__items): item =self.__items[self.__index] self.__index +=1 return item else: raise StopIteration python author: John Doe link: http://example.com/2020/10/05/自定义实现迭代器/ Copyright notice: All articles on this website, unless otherwise stated, adopt CC BY-NC-ND 4.0 reprint policy. If reproduced, please indicate source! Prev: 商标网信息爬取 Next: 爬取猫眼电影top100 你的赏识是我前进的动力 支付宝 微信