array slices in python

Andrew McLean alm1 at at-andros.demon.co.uk
Tue May 13 18:14:27 EDT 2003


In article <3064b51d.0305130843.e040c0c at posting.google.com>, 
beliavsky at aol.com writes
>> Fortran does things differently, sure, but computer science has moved
>> on.  Fortran doesn't index arrays from zero, which there are lots of
>> good computer science reasons to do (i.e., offsets into arrays); the
>> only pressing reason for indexing from one is because that's the way
>> some mathematical formulae are written, but that's not a very convincing
>> argument since it's a trivial substitution to change a [1, n] range to a
>> [0, n) one.
>
>By default, Fortran indexes arrays from zero, but you can have arrays
>with other offsets. After the Fortran 95 declaration
>
>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).

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. 
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 
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.

-- 
Andrew McLean





More information about the Python-list mailing list