[Python-ideas] list.index() extension

Steven D'Aprano steve at pearwood.info
Sun Apr 5 05:01:56 CEST 2009


On Sun, 5 Apr 2009 07:38:50 am Benjamin Peterson wrote:

> def index(self, obj, predicate=operator.eq):
>     for idx, item in enumerate(self):
>         if predicate(item, obj):
>             return idx
>     raise IndexError


Isn't that a misuse of IndexError?

>>> [1, 2, 3].index(5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.index(x): x not in list

>>> [1, 2, 3][4]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range



-- 
Steven D'Aprano



More information about the Python-ideas mailing list