[Python-ideas] list.index() extension

Aahz aahz at pythoncraft.com
Sun Apr 5 03:55:34 CEST 2009

On Sat, Apr 04, 2009, Benjamin Peterson wrote:
> I would like to propose a extra parameter `predicate` to list.index()
> like this:
> def index(self, obj, predicate=operator.eq):
>     for idx, item in enumerate(self):
>         if predicate(item, obj):
>             return idx
>     raise IndexError
> My main use-case is 2to3 where a node has to locate its self in
> the parents node list by identity. Instead of writing out the
> search manually as is done now, it would be nice to just write
> `self.parent.nodes.index(self, operator.is_)`.

-1 -- it complicates the documentation too much for a small feature.
Your function looks just fine, and I see no reason to add a new method.
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it."  --Brian W. Kernighan

More information about the Python-ideas mailing list