This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Usui
Recipients Usui, amaury.forgeotdarc, andersk, eric.araujo, georg.brandl, pitrou, r.david.murray, rhettinger, terry.reedy, ysj.ray
Date 2016-10-17.13:14:25
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1476710065.94.0.17516063316.issue8376@psf.upfronthosting.co.za>
In-reply-to
Content
A new proposal is to add at the end of the paragraph on the iterator those point:
"""
As you should have noticed this Reverse let you iterate over the data only once::

   >>> rev = Reverse('spam')
   >>> for char in rev:
   ...     print(char)
   ...
   m
   a
   p
   s
   >>> for char in rev:
   ...     print(char)
   ...
   >>>

So now let's complete our example to have a container that allow you to iterate over his items backwards::

  class ReverseList(object):
       "Container that lets you iterate over the items backwards"
       def __init__(self, data):
           self.data = data
       def __iter__(self):
           return ReverseIterator(self.data)

   class ReverseIterator(object):
       "Iterator for looping over a sequence backwards"
       def __init__(self, data):
           self.data = data
           self.index = len(data)
       def __iter__(self):
           return self
       def next(self):
           if self.index == 0:
               raise StopIteration
           self.index = self.index - 1
           return self.data[self.index]

   >>> rev = ReverseList([1,2,3])
   >>> for char in rev:
   ...     print (char)
   ...
   3
   2
   1
   >>> for char in rev:
   ...     print (char)
   ...
   3
   2
   1
"""
History
Date User Action Args
2016-10-17 13:14:26Usuisetrecipients: + Usui, georg.brandl, rhettinger, terry.reedy, amaury.forgeotdarc, pitrou, eric.araujo, r.david.murray, ysj.ray, andersk
2016-10-17 13:14:25Usuisetmessageid: <1476710065.94.0.17516063316.issue8376@psf.upfronthosting.co.za>
2016-10-17 13:14:25Usuilinkissue8376 messages
2016-10-17 13:14:25Usuicreate