[Python-Dev] Dafanging the find() gotcha
Guido van Rossum
Mon, 05 Aug 2002 15:25:51 -0400
> How many are relying on '' in 'abc' raising an exception?
That's impossible to know.
The case that I am familiar with is roughly as follows. Suppose you
want to check whether a string begins with a certain character, and
you write something like this:
c = s
...do stuff with c...
if c in string.letters:
...parse it further...
The first time this is called with s being empty, the assignment to c
fails because the empty string doesn't have a first item.
So you "fix" that by changing it to this:
c = s[:1]
But the code is still broken. Currently, the "if c in string.letters"
will raise an exception, and you'll figure out that s=="" should be
special-cased earlier on. With the proposed "in" semantics, this
failure is only detected when the "parse it further" code does the
wrong thing -- either it raises another exception, or it produces the
wrong result without raising an exception. I expect that that will be
harder to debug because the source of the error is farther away from
Note that we make similar exceptions for the empty string in other
Somehow this reminds me of the 0**0 debate recently in edu-sig...
--Guido van Rossum (home page: http://www.python.org/~guido/)