[Python-ideas] Deprecate str.find

Mike Graham mikegraham at gmail.com
Sat Jul 16 19:43:37 CEST 2011


On Sat, Jul 16, 2011 at 11:46 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
> Raymond Hettinger 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
>> <http://www.google.com/codesearch#search/&q=%5C.find%5C(%20lang:%5Epython$&type=cs>
>
>
> How ironic that the fist hit seems to display the problem Mike is concerned
> with:
>
>        position =  min(position, len(self.contents))
>        if hasattr(newChild, 'parent') and newChild.parent != None:
>            # We're 'inserting' an element that's already one
>            # of this object's children.
>            if newChild.parent == self:
>                index = self.find(newChild)
>                if index and index < position:
>                    # Furthermore we're moving it further down the
>                    # list of this object's children. That means that
>                    # when we extract this element, our target index
>                    # will jump down one.
>                    position = position - 1
>
> 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()?
>
> ~Ethan~

Note that this particular code is using
BeautifulSoup.PageElement.find, not str.find. There are, however, in
the first few pages similar snippets which use the result of str.find
without a check for the special case.

Mike



More information about the Python-ideas mailing list