Why Python does *SLICING* the way it does??

Torsten Bronger bronger at physik.rwth-aachen.de
Wed Apr 20 12:24:45 EDT 2005


Hallöchen!

Bernhard Herzog <bh at intevation.de> writes:

> Torsten Bronger <bronger at physik.rwth-aachen.de> writes:
>
>>> http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF
>>
>> I see only one argument there: "Inclusion of the upper bound
>> would then force the latter to be unnatural by the time the
>> sequence has shrunk to the empty one."  [...]
>
> The other main argument for startig at 0 is that if you do not
> include the upper bound and start at 1 then the indices i of a
> sequence of N values are 1 <= i < N + 1 which is not as nice as 0
> <= i < N.  opportunity for an off by one error.

The alternative is starting with 1 and using "lower <= i <= upper".
(Dijkstra's second choice.)

> Then there's also that, starting at 0, "an element's ordinal
> (subscript) equals the number of elements preceding it in the
> sequence."

Granted, but you trade such elegancies for other uglinesses.  A
couple of times I changed the lower limit of some data structure
from 0 to 1 or vice versa, and ended up exchanging a "+1" here for a
"-1" there.

It's a matter of what you are accustomed to, I suspect.  We
(programmers) think with the 0-notation, but non-spoiled minds
probably not.

Tschö,
Torsten.

-- 
Torsten Bronger, aquisgrana, europa vetus



More information about the Python-list mailing list