__setitem__ used in a for loop. : setitem « Class « Python Tutorial






class MyRange:
   def __init__( self, start, stop, step ):
      self.__sequence = range( start, stop, step )
      self.__nextValue = 0  

   def __iter__( self ):
      return self

   def next( self ):
      try:      
         value = self.__sequence[ self.__nextValue ]
      except IndexError:
         raise StopIteration
      else:
         self.__nextValue += 1
         return value

range1 = MyRange( 0, 10, 1 )

for value in range1:
   print value,

print      

range2 = MyRange( 0, 10, 1 )
range2Iterator = iter( range2 )  # retrieve iterator for range2

while 1:
   try:
      value = range2.next()
   except StopIteration:
      break
   else:
      print value,

print         

range3 = MyRange( 0, 10, 1 )
range3Iterator1 = iter( range3 )
range3Iterator2 = iter( range3 )

for i in range( 10 ):
   print "Loop iteration %d: range3Iterator1.next() = %d" % ( i, range3Iterator1.next() )
   print








11.27.setitem
11.27.1.__setitem__ used in a for loop.