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

Antoon Pardon apardon at forel.vub.ac.be
Mon Aug 29 04:47:52 EDT 2005


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.

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.

-- 
Antoon Pardon



More information about the Python-list mailing list