我正在尝试扩展列表类型以仅采用整数。我确实覆盖了append,setitem但它不起作用。请问有什么指点吗?
class linked_list(list):
def __init__(self):
self.indx = 0
def next(self):
self.indx += 1
return self.__getitem__(self.indx)
def prev(self,indx):
self.indx -= 1
return self.__getitem__(self.indx)
def __append__(self,val):
if not isinstance(val,int):
raise Exception('only integers accepted')
super(linked_list,self).__append__(val)
def __getitem__(self,indx):
try:
return super(linked_list,self).__getitem__(indx)
except IndexError,e:
StopIteration()
def __setitem__(self,key,val):
if not isinstance(val,int):
raise Exception('only integers accepted')
super(linked_list,self).__setitem__(key,val)