class mylist(list):
def index_withoutexception(self,i):
try:
return self.index(i)
except:
return -1
因此,您可以使用 list,并在 index2中返回出现错误时所需的内容。
你可以这样使用它:
l = mylist([1,2,3,4,5]) # This is the only difference with a real list
l.append(4) # l is a list.
l.index_withoutexception(19) # return -1 or what you want
I like to use Web2py 的名单 class, found in the storage module of its gluon package. The storage module offers list-like (List) and dictionary-like (Storage) data structures that do not raise errors when an element is not found.