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

Steve Holden steve at holdenweb.com
Mon Aug 29 17:28:22 EDT 2005


Antoon Pardon wrote:
> Op 2005-08-27, Steve Holden schreef <steve at holdenweb.com>:
> 
>>>
>>If you want an exception from your code when 'w' isn't in the string you 
>>should consider using index() rather than find.
> 
> 
> Sometimes it is convenient to have the exception thrown at a later
> time.
> 
> 
>>Otherwise, whatever find() returns you will have to have an "if" in 
>>there to handle the not-found case.
> 
> 
> And maybe the more convenient place for this "if" is in a whole different
> part of your program, a part where using -1 as an invalid index isn't
> at all obvious.
> 
> 
>>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.
> 
> 
> You always seem to look at such things in a very narrow scope. You never
> seem to consider that various parts of a program have to work together.
> 
Or perhaps it's just that I try not to mix parts inappropriately.

> So what happens if you have a module that is collecting string-index
> pair, colleted from various other parts. In one part you
> want to select the last letter, so you pythonically choose -1 as
> index. In an other part you get a result of find and are happy
> with -1 as an indictation for an invalid index. Then these
> data meet.
> 
That's when debugging has to start. Mixing data of such types is 
somewhat inadvisable, don't you agree?

I suppose I can't deny that people do things like that, myself included, 
but mixing data sets where -1 is variously an error flag and a valid 
index is only going to lead to trouble when the combined data is used.

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