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

Steve Holden steve at holdenweb.com
Fri Aug 26 22:13:30 EDT 2005


Bryan Olson wrote:
> 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.
> 
> 
If you want an exception from your code when 'w' isn't in the string you 
should consider using index() rather than find.

Otherwise, whatever find() returns you will have to have an "if" in 
there to handle the not-found case.

This just sounds like whining to me. If you want to catch errors, use a 
function that will raise an exception rather than relying on the 
invalidity of the result.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC             http://www.holdenweb.com/




More information about the Python-list mailing list