[Python-ideas] Deprecate str.find

Steven D'Aprano steve at pearwood.info
Sun Jul 17 05:17:42 CEST 2011


Ethan Furman wrote:

> I haven't read all the surrounding code to know if this will ever fail, 
> but the whole 'index = ... .find(...); if index and ...' certainly 
> doesn't lend confidence.  After all, if you *know* newChild is in self, 
> why not use .index()?

Perhaps because the name "find" tells you exactly what the method does, 
while the name "index" is ambiguous. Does it mean "what is at this 
index?" or "what index is this at?".

I've occasionally seen people mistakingly write mylist.index(i) instead 
of mylist[i]. Including an experienced Python coder who did know better. 
In that case, it was just a thinko (like a typo, only in the brain 
*wink*), but in my opinion, the name "index" is not a good name.

Since find and index are equally efficient when the substring is 
present, there's no speed advantage to choosing one over the other if 
you know that the substring is present. In my opinion str.find beats 
str.index for readability so comprehensively that there is no contest -- 
I would *always* use find if available.

In my wishlist for Python 4000 I have:

* list.index renamed to list.find
* str.find and list.find return None if the argument is not found




-- 
Steven



More information about the Python-ideas mailing list