Why are slice indices the way they are in python?

Ant antroy at gmail.com
Thu Nov 30 09:35:16 CET 2006


Steve Bergman wrote:

...
> Why?  It doesn't seem intuitive to me.  To me, it makes it harder, not
> easier, to work with slices than if indexing started at 1 and  the
> above expression got you the 2nd throught the 5th character.

Dijkstra has an article about this:
http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html

In practice it probably wouldn't make a great deal of difference. Here
at my current job, we have a situation where a bunch of Progress 4GL
programmers have been taught Java and let loose on the code base. we
now have a situation where there are a plethora of methods written
designed to be 1-based, and you have to trawl through the code to work
out if the method was written by a Java programmer or a 4GL programmer
to save on array index exceptions...

The infuriating thing is that if you are iterating over an array in
Java, and have to use one of the functions based on an array index, you
have to use function(i + 1). Drilling back through the code, and
invariably you have the function written:

def function(i):
    return array[i-1]

!!! (at least you would if the Java was written in Python by 4GL
programmers, but you get the idea ;-)  )




More information about the Python-list mailing list