Python Feature Request: Allow changing base of member indices to 1

D

Dan Bishop

The FORTRAN family had started as 1-based (F95, and Ada, now allow
for each array to have its own "base" => x : array (-10..10) of float).
Pascal, I forget...

Pascal allows arbitrary array bases. It's where Ada got the idea.
 
S

Steven D'Aprano

Pascal allows arbitrary array bases. It's where Ada got the idea.

It does? Since when?

When I was being taught Pascal at University (20 years ago...) Pascal
always started indices at 1. We used to joke you could tell musicians who
were trained as C programmers from those who were Pascal programmers
because they did sound checks "Testing, 0 1 2".
 
A

Alex Martelli

Steven D'Aprano said:
It does? Since when?

Ever since Pascal existed, the syntax has been "array[lower..upper] of
sometype" -- no default value for lower (neither 0 nor 1 nor other).


Alex
 
S

Steven D'Aprano

Steven D'Aprano said:
It does? Since when?

Ever since Pascal existed, the syntax has been "array[lower..upper] of
sometype" -- no default value for lower (neither 0 nor 1 nor other).

*slaps head*

Of course it does! D'oh!

We always used to write array[1..n] for an n item array, which is a
convention, not enforced by the compiler.
 
N

Nick Craig-Wood

Sherm Pendley said:
Quite right.


When was the last time you used Perl? It was allowed in Perl 4 and earlier,
because many Perl users were moving from Awk, which uses an array base of 1.
Even then, having multiple index start points within a single program wasn't
the idea; the idea was to allow programs originally written in Awk to be
ported to Perl with minimal updating.

It was deprecated as of 5.0 though - which was released in '94. It still
works, for the sake of backwards compatibility, but its use in new code is
highly discouraged.

I seem to remember from "Programming Perl" that Larry Wall said it was
a serious mistake to add this feature to perl.

If it is a feature too far for perl then it is *definitely* a feature
too far for python ;-)
 
J

Javier Bezos

Here is a document giving good reasons for indexing to start at
zero, as in Python.
http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
The author has done a bit:
http://en.wikipedia.org/wiki/Dijkstra

Dijkstra's argument is obsolete, as it is based on
how array length was computed many years ago -- if
we have an array a = b..e, then the lenght of a
is e-b (half open range). Good at low level
programming.

But a quarter of a century after we know concepts
are much better than low level programming and
explicit computations -- if we have an array
a = b..e, then the length of a should be a.length()
(or a.length(b,e)), and it is independent of
arbitrary ranges, index bases, or even steps
(eg, {-4, -2, 0, 2, 4}).

Of course, the index base should be always the
same _by default_ (individual lists could require
another index base, and that's fine). Otherwise
it would a mess, as you said.

Javier
 
P

Paddy

Dijkstra's argument is obsolete, as it is based on
how array length was computed many years ago -- if
we have an array a = b..e, then the lenght of a
is e-b (half open range). Good at low level
programming.

But a quarter of a century after we know concepts
are much better than low level programming and
explicit computations -- if we have an array
a = b..e, then the length of a should be a.length()
(or a.length(b,e)), and it is independent of
arbitrary ranges, index bases, or even steps
(eg, {-4, -2, 0, 2, 4}).

Of course, the index base should be always the
same _by default_ (individual lists could require
another index base, and that's fine). Otherwise
it would a mess, as you said.

Javier
-----------------------------http://www.texytipografia.com

Hi Javier,
You seem to have missed out array *indexing*
in your argument, or is array indexing obsolete?

- Paddy.
 
B

Beliavsky

So the running count is:
Ayes to the left: VB compatibility.
Nays to the right: QuadIO, Perl, Dijkstra paper.

The nays have it!

One-based indexing would also Python more compatible with Fortran,
Matlab/Octave/Scilab, and S (the language of S-Plus and R). It appears
that engineers, scientists, and statisticians, as opposed to
professional programmers, like 1-based indexing. An obvious argument
for 1-based indexing in the FORmula TRANslation programming language
is that formulas involving arrays in textbooks almost always use 1-
based indexing.

Since Python has always had 0-based indexing and since a user-defined
base can cause problems, as has been discussed, I think Python and
extensions such as NumPy should be left as is.
 
D

Dan Bishop

One-based indexing would also Python more compatible with Fortran,
Matlab/Octave/Scilab, and S (the language of S-Plus and R). It appears
that engineers, scientists, and statisticians, as opposed to
professional programmers, like 1-based indexing. An obvious argument
for 1-based indexing in the FORmula TRANslation programming language
is that formulas involving arrays in textbooks almost always use 1-
based indexing.

I've seen plenty of examples of zero-based indexing in math
textbooks. For example, the coefficients of polynomials and Fourier
cosine series, both of which use a zero subscript for the constant
term.
 
J

Javier Bezos

Paddy,
Hi Javier,
You seem to have missed out array *indexing*
in your argument, or is array indexing obsolete?

Of course, it isn't, but it has evolved over the
past 25 years. When Djikstra wrote that, many
people (including me) was using a Spectrum---
Now we have OO programming to deal with concepts
and a more generic programming. So, to me it's
clear his _argument_ is very outdated and cannot
be applied directly and withot reservations to
modern languages like Python.

Javier
 
D

Dustan

I don't think it's worth the hassle. BTW, what's, IYHO, the distinct
advantage of starting array indices at 1?

For newbies, it's easier to count starting with 1. It's rather
unintuitive to start at 0.

That's not to say that I support this feature request; I got used to
counting from 0. It just took me some time.
 
Z

Zara

Paddy,



Of course, it isn't, but it has evolved over the
past 25 years. When Djikstra wrote that, many
people (including me) was using a Spectrum---
Now we have OO programming to deal with concepts
and a more generic programming. So, to me it's
clear his _argument_ is very outdated and cannot
be applied directly and withot reservations to
modern languages like Python.


One language created by and for mathematicians: Haskell.
It uses zero based list indexing (the most similar to array indexing
they have), so that list!!0 is the first element and list!!3 the
fourth element.

And they tend to reason in Dijkstra's style. They love natural numbers
beginning with zer:

zara
 
A

Antoon Pardon

This is like the previous one. Please check for sanity and approve for
posting at python-dev.

I would like to have something like "option base" in Visual Basic.
IIRC it used to allow me to choose whether 0 or 1 should be used as
the base of member indices of arrays. In Python, the same can be used
with strings, lists, tuples etc.

This would mean:
foo = "foo"
=> foo[1] == 'f'

foo = ['foo', 'bar', 'spam' ]
=> foo[1] == 'foo'

foo = ('spam', 'eggs')
=> foo[1] == 'spam'

For convenience it should also affect the range function so that:

range(3) = [1, 2, 3]

because this is often used where arrays would be used in VB.

Finally, when the programmer does not specify his choice of base at
the beginning of the program, the current behaviour of using 0 as base
should continue so that there is no problem with backward
compatibility.

Here is a document giving good reasons for indexing to start at
zero, as in Python.

I find he just picks the reasons he agrees with.

If you pick your values as a <= i <= b it has the advantage that
the bounds are explicit. No need to add or substract 1 from
one of the values to get the exact boundis. Now how much weight
you want to give this characteristic is open for debate but
the fact that it isn't even mentioned doesn't give me much
confidence in the author's ability to weight all the pro's
and con's.
http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
The author has done a bit:
http://en.wikipedia.org/wiki/Dijkstra

Having more than one index start point would be a maintenance
nightmare best avoided. (It can be done in Perl).

It was never a problem when I still programmed in Pascal.
 
B

Bjoern Schliessmann

Dustan said:
For newbies, it's easier to count starting with 1. It's rather
unintuitive to start at 0.

Cool. So Python will become a "newbie language", and everyone
that "jumps" from Python to a different language will have to
relearn 0-based indices? ;)

Regards,


Björn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,132
Latest member
TeresaWcq1
Top