Python "why" questions

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Aug 19 15:32:41 EDT 2010


On Thu, 19 Aug 2010 11:57:53 -0700, Russ P. wrote:

> I don't
> know where zero-based indexing started, but I know that C used it very
> early, probably for some minuscule performance advantage.

In C, zero based indexing was used because it made pointer arithmetic 
elegant and reduced bugs.


> When C++ came
> along, it tried to be somewhat compatible with C, so it continued using
> zero-based indexing.

And because a feature that isn't broken doesn't need to be "fixed".

> Then Java was loosely modeled after C++, so the
> convention continued. Python was written in C, so zero-based indexing
> was "natural." So the whole thing is based on a decision by some guy who
> was writing a language for operating systems, not mathematics or
> application programming.

Python is vastly different from C. It has just as many similarities to 
Pascal as C, which uses 1-based indexing. No surprise there -- Python, 
like both Pascal and C, is a member of the Algol family of languages.

It is interesting to see that Nicholas Wirth's first language, Pascal, 
used one-based indexing, and his latest, Oberon, uses zero-based. Say 
what you like about Wirth, but he's not influenced by the desire to be 
like C. Oberon uses the same zero-based half-open on the right indexing 
as Python:

VAR a: ARRAY 10 OF INTEGER;

creates an array a[0]...a[9].

If Wirth has moved to the Python convention, it is because it *works*, 
not because he's trying to ape C.

Not all languages are based on Algol. Languages derived from Lisp also 
start array indexing at 0. So does Forth. So that's at least three 
significantly different language families which have independently 
converged on zero-based indexing.


[...]
> I wouldn't have guessed that APL is a "mainstream" language. 

I said "slightly". It was surprisingly popular in it's time, and there 
are still multiple APL compilers and interpreters for Windows, Linux and 
Mac.


-- 
Steven



More information about the Python-list mailing list