Rozpoczynam nauke Pythona i analizuje kod do odwrocenia znakow:
#!/usr/bin/python
class Reverse:
def __init__(self,data):
self.data = []
self.index = len(data)
def __iter__(self):
return self
def next(self):
if self.index == 0:
raise StopInteraction
self.index = self.index - 1
return self.data[self.index]
rev = Reverse('spam')
for char in rev:
print char
otrzymalam nastepujacy blad, ktory chwilowo nie wiem jak poprawić:
Traceback (most recent call last):
File "./reverse.py", line 18, in
for char in rev:
File "./reverse.py", line 13, in next
return self.data[self.index]
IndexError: list index out of range
Mam równieź pytanie odnośnie pracy metody iter i next.
Jeśli dobrze rozumiem, to iter wywołuje wszystkie elemety tablicy data.
Jak jest wywolywana metoda next? Czy nie musze jej wywolac jawnie, next() ?
Będę niezmiernie wdzięczna za wyjaśnienie tych problemów.