Another Wart! string.find() [ was: namespace issue? ]

Tim Peters tim.one at home.com
Sat Jun 23 13:47:06 EDT 2001


[Tim]
> Historical note:  at the time these functions were designed, negative
> indices were not allowed:  sequence[negative_int] raised an exception.
> Allowing the latter was a later addition to the language, and was a
> mixed blessing (for this and other reasons; but I think it was a net
> win).

[Chuck Esterbrook]
> I would be interested to know what other things besides the -1 from
> find() were a minus for negative indices...?

Nothing deep:  Python does bounds-checking for sequence indexing, and assorted
cases that are off-by-1 errors in reality (decrementing an index from 0 to -1 by
mistake, or an index difference i-j going negative by mistake) started doing
unexpected things silently-- instead of raising exceptions --after the change.
This makes one class of logic error much harder to track down, and that's a loss
when it happens.  I get bit by this a couple times per year; it's curious how
very rarely it pops up as "the answer" to c.l.py "help!" postings, though.

There was also some minor code breakage, in rare cases where code *relied* on
catching IndexError exceptions to skip over "don't care" conditions (like
using -1 as an explicit "don't care" index in a list of indices -- that simply
doesn't work anymore).

> ...
> I discuss such things because I'm interested in language design and
> Python is closest to what I would consider ideal.

You must be a pragmatist at heart <wink>.

takes-one-to-know-one-ly y'rs  - tim





More information about the Python-list mailing list