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
Wed Aug 31 03:13:12 EDT 2005


Op 2005-08-30, Steve Holden schreef <steve at holdenweb.com>:
> Antoon Pardon wrote:
>> Op 2005-08-29, Steve Holden schreef <steve at holdenweb.com>:
>> 
>>>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.
>> 
>> 
>> I didn't know it was inappropriately to mix certain parts. Can you
>> give a list of modules in the standard list I shouldn't mix.
>> 
>> 
>>>>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?
>> 
>> 
>> The type of both data is the same, it is a string-index pair in
>> both cases. The problem is that a module from the standard lib
>> uses a certain value to indicate an illegal index, that has
>> a very legal value in python in general.
>> 
> Since you are clearly feeling pedantic enough to beat this one to death 
> with a 2 x 4 please let me substitute "usages" for "types".

But it's not my usage but python's usage.

> In the case of a find() result -1 *isn't* a string index, it's a failure 
> flag. Which is precisely why it should be filtered out of any set of 
> indexes. once it's been inserted it can no longer be distinguished as a 
> failure indication.

Which is precisely why it was such a bad choice in the first place.

If I need to write code like this:

  var = str.find('.')
  if var == -1:
    var = None

each time I want to store an index for later use, then surely '-1'
shouldn't have been used here.


>>>I suppose I can't deny that people do things like that, myself included, 
>> 
>> 
>> It is not about what people do. If this was about someone implementing
>> find himself and using -1 as an illegal index, I would certainly agree
>> that it was inadvisable to do so. Yet when this is what python with
>> its libary offers the programmer, you seem reluctant find fault with
>> it.

> I've already admitted that the choice of -1 as a return value wasn't 
> smart. However you appear to be saying that it's sensible to mix return 
> values from find() with general-case index values.

I'm saying it should be possible without a problem. It is poor design
to return a legal value as an indication for an error flag.

> I'm saying that you 
> should do so only with caution. The fact that the naiive user will often 
> not have the wisdom to apply such caution is what makes a change desirable.

I don't think it is naive, if you expect that no legal value will be
returned as an error flag. 

>>>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.
>> 
>> 
>> Yet this is what python does. Using -1 variously as an error flag and
>> a valid index and when  people complain about that, you say it sounds like
>> whining.
>> 
> What I am trying to say is that this doesn't make sense: if you want to 
> combine find() results with general-case indexes (i.e. both positive and 
> negative index values) it behooves you to strip out the -1's before you 
> do so. Any other behaviour is asking for trouble.

I would say that choosing this particular return value as an error flag
was asking for trouble. My impression is that you are putting more
blame on the programmer which fails to take corrective action, instead
of on the design of find, which makes that corrective action needed
in the first place.

-- 
Antoon Pardon



More information about the Python-list mailing list