[Python-ideas] Deprecate str.find

Mike Graham mikegraham at gmail.com
Sat Jul 16 21:26:56 CEST 2011


On Sat, Jul 16, 2011 at 3:07 PM, Raymond Hettinger
<raymond.hettinger at gmail.com> wrote:
> Deprecation is a step towards removal and it always causes a certain amount of pain.
>
> ISTM, the time for any move like this would have been the jump from Python 2 to Python 3 where significant breakage was expected and where transition tools were developed.

Because this would break backwards compatibility unnecessarily, I
think anyone involved can agree that actual removal could not take
place until a special jump akin to the Python 2->Python 3 jump. (It
would surprise me if such a jump ever actually took place.)

>> I appreciate your linking this search, which does indeed does show
>> that str.find is in wide use. However, looking at the first give pages
>> of results, this use seems largely unfortunate—literally the majority
>> of the times str.find is used, I would have used "substring in s" or
>> "s.startswith(substring)". I also see code like "pos = s.find("  (");
>> if pos + len(" (...)") > self._maxWidth:" which makes me very
>> uncomfortable and which I would have to read in detail to figure out
>> what's happening confidently if I was the maintaining the code.
>
> I am largely unsympathetic to arguments that are
> roughly equivalent to "I don't like the way other people
> write programs".

Reviewing five pages of results, over 2/3 of the uses of str.find
could be replaced by str.__contains__. Using sub in s instead of
s.find(sub) != -1 is *already* the advice in the official Python
documentation, so I do not believe I am making especially personal
judgments about the style of the code.

> Something akin to the str.find() API has been present in many,
> many languages for a very long time.  For the most part, people
> seem to be able to use it reasonably well.  I find that beginning
> Python students never seem to have a problem with it.

I'm glad your experience with learners here has been more consistent than mine.

> Also keep it mind that startswith() and endswith() were relatively
> recent additions to Python, so it is no surprise that lots of code
> uses find() instead of startswith().
>
>
> Raymond

Mike



More information about the Python-ideas mailing list