I am trying to figure this problem out but I can't seem to get the code right. Please help.
OUESTION:
Implement a subclass of list called myList. It will behave just like the list class except for iteration:
Usage:
>>> l = myList([3,5,7,9])
>>> for item in l:
print(item)
3
8
15
24
Explanation: the iteration is actually over all the partial sums of list l:
* 3,
* 3+5,
* 3+5+7, and
* 3+5+7+9.
MY CODE:
class myList(list):
def __iter__(self):
return ListIterator(self)
class ListIterator(object):
def __init__(self, lst):
self.lst = lst
self.index = 0
def __add__(self, ):
if self.index >= len(self.lst):
raise StopIteration
res = self.lst[self.index]
self.index += 2
return res