[Python-ideas] Deprecate str.find

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


Mike Graham wrote:
> On Sat, Jul 16, 2011 at 11:26 AM, Raymond Hettinger
> <raymond.hettinger at gmail.com> wrote:
>> Unless an API is flat out broken, deprecation is almost always a bad idea.
>> This API has been around for a very long time, so deprecating it will
>> break lots of people's code for almost zero benefit:
>> http://www.google.com/codesearch#search/&q=%5C.find%5C(%20lang:%5Epython$&type=cs
>>
>> Raymond
> 
> I agree that breaking people's code is a bad thing and have not
> suggested removing str.find. This removal would require a transition
> like that from Python 2.x to Python 3.x, a move that is not planned
> and I personally do not ever expect.

I think that's a disingenuous thing to say. You started this thread with 
an explicit call to deprecate str.find -- see your chosen subject line, 
and your first paragraph in this thread states:

"It should be explicitly deprecated ..."

What is the point of deprecating something if you don't intend to 
eventually remove it?


> 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

A lot of very old code predates startswith and endswith. They only 
appeared in 2.0. Surprisingly, as late as 2.2, we were limited to 
testing for a single character:

[steve at sylar src]$ python2.2
Python 2.2.3 (#1, Aug 12 2010, 01:08:27)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> "ab" in "abc"
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
TypeError: 'in <string>' requires character as left operand


Some of the code on that first page supports Python 2.1.



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


Whereas "pos = s.index("  ("); if pos + len(" (...)") > self._maxWidth:" 
is the height of readability, yes?

People can write bad code no matter what tools you give them.



-- 
Steven




More information about the Python-ideas mailing list