[Python-ideas] list.index() extension
Guido van Rossum
guido at python.org
Sun Apr 5 20:31:49 CEST 2009
On Sat, Apr 4, 2009 at 6:55 PM, Aahz <aahz at pythoncraft.com> wrote:
> 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.
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-ideas