首页 > 代码库 > python自定义迭代器对象以及可迭代对象

python自定义迭代器对象以及可迭代对象

# coding=utf8from collections import Iteratorfrom collections import Iterable#迭代器对象class OwnIteror( Iterator ):     def __init__(self , arrs ):          self.index = 0          self.arrs = arrs     def  next(self):          if self.index > len( self.arrs ) - 1:              raise StopIteration          else:              self.index +=1              return self.arrs[ self.index - 1 ]#可迭代对象class OwnIterable( Iterable ):      def __init__(self ,  arrs ):          self.arrs = arrs      def __iter__(self):          return OwnIteror( self.arrs )for item in OwnIterable( [ 1,2,3,4,4,6 ] ):      print item

 

python自定义迭代器对象以及可迭代对象