>>> next(it)
'c'
>>> next(it)
-
Traceback (most recent call last):
File "<stdin>", line 1, in ?
next(it)
:meth:`__next__`, then :meth:`__iter__` can just return ``self``::
class Reverse:
- "Iterator for looping over a sequence backwards"
+ """Iterator for looping over a sequence backwards."""
def __init__(self, data):
self.data = data
self.index = len(data)
self.index = self.index - 1
return self.data[self.index]
+::
+
>>> rev = Reverse('spam')
>>> iter(rev)
<__main__.Reverse object at 0x00A1DB50>
for index in range(len(data)-1, -1, -1):
yield data[index]
+::
+
>>> for char in reverse('golf'):
... print(char)
...