Message278805
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
""" |
|
Date |
User |
Action |
Args |
2016-10-17 13:14:26 | Usui | set | recipients:
+ Usui, georg.brandl, rhettinger, terry.reedy, amaury.forgeotdarc, pitrou, eric.araujo, r.david.murray, ysj.ray, andersk |
2016-10-17 13:14:25 | Usui | set | messageid: <1476710065.94.0.17516063316.issue8376@psf.upfronthosting.co.za> |
2016-10-17 13:14:25 | Usui | link | issue8376 messages |
2016-10-17 13:14:25 | Usui | create | |
|