Bug in string.find; was: Re: Proposed PEP: New style indexing,wasRe: Bug in slice type

Terry Reedy tjreedy at udel.edu
Thu Sep 1 12:13:43 EDT 2005


"Stefan Rank" <stefan.rank at ofai.at> wrote in message 
news:4316E5FC.1010806 at ofai.at...
> on 31.08.2005 16:16 Ron Adam said the following:
>> The problem with negative index's are that positive index's are zero
>> based, but negative index's are 1 based.  Which leads to a non
>> symmetrical situations.
>
> Hear, hear.
>
> This is, for me, the root of the problem.

The root of the problem is the misunderstanding of slice indexes and the 
symmetry-breaking desire to denote an interval of length 1 by 1 number 
instead of 2.  Someday, I may look at the tutorial to see if I can suggest 
improvements.  In the meanwhile, see Fredrik's reply and my supplement 
thereto and the additional explanation below.

> But changing the whole of Python to the (more natural and consistent)
> one-based indexing style, for indexing from left and right, is...
> difficult.

Consider a mathematical axis

|_|_|_|_|...
0 1 2 3 4

The numbers represent points separating unit intervals and representing the 
total count of intervals from the left.  Count 'up' to the right is 
standard practice.  Intervals of length n are denoted by 2 numbers, a:b, 
where b-a = n.

Now consider the 'tick marks' to be gui cursor positions.  Characters go in 
the spaces *between* the cursor.  (Fixed versus variable space 
representations are irrelevant here.)  More generally, one can put 'items' 
or 'item indicators' in the spaces to form general sequences rather than 
just char strings.

It seems convenient to indicate a single char or item with a single number 
instead of two.  We could use the average coordinate, n.5.  But that is a 
nuisance, and the one number representation is about convenience, so we 
round down or up, depending on the language.  Each choice has pluses and 
minuses; Python rounds down.

The axis above and Python iterables are potentially unbounded.  But actual 
strings and sequences are finite and have a right end also.  Python then 
gives the option of counting 'down' from that right end and makes the count 
negative, as is standard.  (But it does not make the string/sequence 
circular).

One can devise slight different sequence models, but the above is the one 
used by Python.  It is consistent and not buggy once understood.  I hope 
this clears some of the confusion seen in this thread.

Terry J. Reedy









More information about the Python-list mailing list