Tutorial creates confusion about slices

Michael Hoffman cam.ac.uk at mh391.invalid
Thu Apr 26 10:00:34 EDT 2007


Neil Cerutti wrote:
>> On Apr 23, 1:38 pm, Antoon Pardon <apar... at forel.vub.ac.be> wrote:
>>> The following is part of the explanation on slices in the
>>> tutorial:
>>>
>>> The best way to remember how slices work is
>> ...
>>>   +---+---+---+---+---+
>>>   | H | e | l | p | A |
>>>   +---+---+---+---+---+
>>>   0   1   2   3   4   5
>>>  -5  -4  -3  -2  -1
 >
> I object only to the word "best". I don't like the above model
> because it divorces the indexes that appear in subscripts from
> those that appear in slices. I 't find it complicated to think:
> a[2:4] is the contiguous slice of elements starting at the gap
> between element 1 and 2, and ending at the gap between element 3
> and 4. I've always found thinking of [2:4] as a half-open range
> much easier.
> 
> I suppose the above model could avoid this notational problem if
> you say that a[k] means the one element slice a[k:k+1]
> (technically true for strings, but false for lists), rather than
> ever thinking of item indexes as pointing directly at an item.
> 
> So I vote that the word "best" be removed.

I agree. It would be better to say that, "One way to help you understand 
how slices work is to think of..."
-- 
Michael Hoffman



More information about the Python-list mailing list