array slices in python

Alex Martelli aleax at aleax.it
Wed May 14 05:08:15 EDT 2003


Andrew McLean wrote:
   ...
>>real :: x(3),y(-1:2)
>>
>>x has 3 elements and y has 5, starting with y(-1).
> 
> Of course you meant to say that by default Fortran indexes from one (the
> rest I agree with).

Even the fact that y has 5 elements?-)  I think it has four...


> Erik touched on the rationale for the Fortran approach, it's a bit more
> obvious when you remember FORTRAN is short for FORmula TRANslation.
> 
> I have to say I'm not convinced by any of the arguments that any
> approach to this issue is 'more natural', it's really pretty arbitrary.

Personally, I disagree.  Just as defining natural numbers to start
from 0 is 'more natural' (smoother, useful) than defining them to 
start from 1, so it's more natural, smoother, useful to have array
indices start from 0 -- even though it took humanity millennia of
suffering through 1-based number schemes before some Indian genius
finally THOUGHT of zero (there are a few excellent books on the
history of the number zero, btw).

> I suspect that what you think is more natural depends on what you are
> used to, and what the conventions of your field are. Personally, I have

People do often confuse 'natural' with 'habitual', yes.  But even
when I programmed just about only in Fortran, I wasn't fooled.  E.g.
I often had to represent a multi-dimensional array inside a single-
dimensional one -- take 3 dimensions for example.  With 1-based
indexing, given that the lengths of the three axes are ia, ib, ic,
then md(ja,jb,jc) maps to sd(1+(ja-1)+ia*(jb-1+ib*(jc-1)) -- you
can refactor this a bit but the '-1's don't go away, you can almost
'hide' then a bit but they're really there and mess things up.  With
0-based indexing the mapping is to s(ja+ia*(jb+ib*jc)) and there are
NO '-1's nor '+1's to confuse things.  It's not arbitrary...

> done more programming in Fortran and Matlab than anything else, so that
> approach is more familiar, but I can see advantages in the use of half
> open intervals.

Congratulations for avoiding the familiar vs natural confusion, then;-).


Alex





More information about the Python-list mailing list