python设计一个类, 类中一个列表

要求该类的实例化对象为可迭代对象,并要求每次调用next函数时,自动输出data中当前元素与下一个元素的和(假如data中有10个元素,那么肯定只能输出前9项的操作结果),并在抛出异常后对异常进行处理,输出"All elements are printed."

请问这个要怎么写?

给个简单的例子参考:

class MyClass():
    def __init__(self, lst):
        self.lst = lst
        self.index = 0
        self.length = len(lst) 
    
    def next(self):
        if self.index<self.length-1:
            print(self.lst[self.index]+self.lst[self.index+1])
            self.index += 1
        else:
            print('All elements are printed')

if __name__=='__main__':
    lst = [0,1,2,3,4,5,6,7,8,9]
    mc = MyClass(lst)
    for i in range(len(lst)):
        mc.next()

img


class data(object):
    def __init__(self):
        self.counter = 0
    def __iter__(self):
        return  self
    def __next__(self):
        self.counter += 1
        if self.counter == 10:
            raise StopIteration("All elements are printed.")
        return self.counter

from typing import Iterable
class IterList:
    
    def __init__(self, it:Iterable=[]) -> None:
        self.it = it
        self.counter = 0

    def __next__(self):
        if self.counter == len(self.it) - 1:
            raise StopIteration("All elements are printed.")
        
        res = self.it[self.counter] + self.it[self.counter+1]
        self.counter += 1
        return res
    
    def __iter__(self):
        
        return self
    

it = IterList([1,2,3])

for i in it:
    print(i)

如果满意,请采纳!