Bug in string.find; was: Re: Proposed PEP: New style indexing, was Re: Bug in slice type

Bryan Olson fakeaddress at nowhere.org
Fri Aug 26 14:46:27 EDT 2005


Steve Holden wrote:
 > Bryan Olson wrote:
 >> Antoon Pardon wrote:

 >>  > It probably is too late now, but I always felt, find should
 >>  > have returned None when the substring isn't found.
 >>
 >> None is certainly a reasonable candidate.
[...]
 >> The really broken part is that unsuccessful searches return a
 >> legal index.
 >>
 > We might agree, before further discussion, that this isn't the most
 > elegant part of Python's design, and it's down to history that this tiny
 > little wart remains.

I don't think my proposal breaks historic Python code, and I
don't think it has the same kind of unfortunate subtle
consequences as the current indexing scheme. You may think the
wart is tiny, but the duct-tape* is available so let's cure it.

[*] http://www.google.com/search?as_q=warts+%22duct+tape%22


 >> My suggestion doesn't change what find() returns, and doesn't
 >> break code. Negative one is a reasonable choice to represent an
 >> unsuccessful search -- provided it is not a legal index. Instead
 >> of changing what find() returns, we should heal the
 >> special-case-when-index-is-negative-in-a-certain-range wart.
 >>
 >>
 > What I don't understand is why you want it to return something that
 > isn't a legal index.

In this case, so that errors are caught as close to their
occurrence as possible. I see no good reason for the following
to happily print 'y'.

     s = 'buggy'
     print s[s.find('w')]

 > Before using the result you always have to perform
 > a test to discriminate between the found and not found cases. So I don't
 > really see why this wart has put such a bug up your ass.

The bug that got me was what a slice object reports as the
'stop' bound when the step is negative and the slice includes
index 0. Took me hours to figure out why my code was failing.

The double-meaning of -1, as both an exclusive stopping bound
and an alias for the highest valid index, is just plain whacked.
Unfortunately, as negative indexes are currently handled, there
is no it-just-works value that slice could return.


-- 
--Bryan



More information about the Python-list mailing list