[Python-3000] Droping find/rfind? (original) (raw)

Josiah Carlson jcarlson at uci.edu
Wed Aug 23 23:53:05 CEST 2006


"BJörn Lindqvist" <bjourne at gmail.com> wrote:

On 8/23/06, Josiah Carlson <jcarlson at uci.edu> wrote: > or even > > index = 0 > while 1: > index = text.find(..., index) > if index == -1: > break > ... > compared with > > index = 0 > while 1: > try: > index = text.index(..., index) > except ValueError: > break > ... You are supposed to use the in operator: index = 0 while 1: if not "something" in text[index:]: break

This can also lead to O(n^2) running time, causes unnecessary string allocation, memory copies, etc. If I saw that in real code, I'd probably lose respect for the author of that module and/or package.

IMHO, removing find() is good because index() does the same job without violating the Samurai Principle (http://c2.com/cgi/wiki?SamuraiPrinciple). It would be interesting to see the patch that replaced find() with index(), did it really make the code more cumbersome?

Everywhere there is a test for index==str.find(...), needs to be replaced with a try/except clause. That's a cumbersome translation if there ever was one.



More information about the Python-3000 mailing list