[Python-Dev] Dafanging the find() gotcha
Tim Peters
tim.one@comcast.net
Mon, 05 Aug 2002 22:18:31 -0400
[Andrew Koenig]
> ...
> Nevertheless, you have not convinced me that this distinction
> is useful in this context.
That will have to wait until it burns you in practice.
> I will agree with you that (a) Many times, people search for literals
> in strings, and (b) it is hard to imagine why an empty literal would
> be useful.
>
> However, that says nothing to me about why an expression of the form
> (s in t) should be considered an error when s has no characters.
Nor should it -- literals are the simplest form of expression, used here
just for concreteness. The question for you is, *however* the value of s
was obtained, if you end up doing "s in t" when s happens to be an empty
string, is it more likely that your program has strayed from your intent, or
that a result of True *was* your intent?
if s[j+k1:j+k2] in t:
Assuming type correctness, if I know that raises an exception whenever k1 >=
k2, then I have confidence I know what the code is trying to do, and rest
easy knowing it won't do something nuts if the index expressions go crazy.
If instead it never(!) raises an exception, no matter what the values of j,
k1 and k2, this code scares me.
When Python switched to allowing negative indices as sequence subscripts (it
didn't always -- they used to raise exceptions), it introduced a nasty class
of bug caused by conceptually non-negative indices going negative by
mistake, but no longer complaining. Overall I think negative indices added
enough expressiveness to outweigh that drawback, but it was far from a pure
win. This is a case where we're also keen to make a formerly exceptional
operation "mean something", but there's one particular case of it where I
know doing so will create similar new problems -- and it's a case that's of
no *real* use to allow.
> And it says even less about why it would be a good idea to have
> the result of such a search yield different results in different
> contexts.
I agree that's not a good thing at all, and it may well win Guido in the
end. I just hope he feels rotten about it, because the children will suffer
as a result <wink>.