Bug in string.find; was: Re: Proposed PEP: New style indexing, was Re: Bug in slice type
apardon at forel.vub.ac.be
Tue Aug 30 03:12:05 EDT 2005
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
>>>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.
> 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
> 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
More information about the Python-list