[Python-ideas] Deprecate str.find

Raymond Hettinger raymond.hettinger at gmail.com
Sat Jul 16 21:07:19 CEST 2011


On Jul 16, 2011, at 10:39 AM, Mike Graham wrote:

> I agree that breaking people's code is a bad thing and have not
> suggested removing str.find.

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.

> 
> 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".

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.

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


More information about the Python-ideas mailing list