Python "why" questions
andrei.avk at gmail.com
Thu Aug 19 21:18:23 CEST 2010
On 08/19/2010 02:04 PM, Steven D'Aprano wrote:
> On Tue, 17 Aug 2010 19:15:54 -0700, Russ P. wrote:
>> The convention of starting with zero may have had some slight
>> performance advantage in the early days of computing, but the huge
>> potential for error that it introduced made it a poor choice in the long
>> run, at least for high-level languages.
> People keep saying this, but it's actually the opposite. Signpost errors
> and off-by-one errors are more common in languages that count from one.
> A simple example: Using zero-based indexing, suppose you want to indent
> the string "spam" so it starts at column 4. How many spaces to you
Doesn't it start at column 5 as shown?
Anyway, it's far more common, I think, to iterate over a sequence and
say "my list is 12 items long. I need to check if I'm on 12th item and
do something special. Doh, I mean, 11th item." In my view, this is far,
far more common, especially for people new to programming, and isn't one
of Python mottos "programming for everybody"? In other words, starting
with 0 leads to implicit error every time you simply pause and think
"at which item am I now and how many items have I got so far?" which are
the two most basic questions involved in iteration.
I'm not saying that 1-th indexing is necessarily better in programming
than 0-th indexing, all things considered. It's an arguable question, it
depends on what kind of operations are more common, and I can't know
this for everyone.
I am saying that it is much more natural for people new to programming
and outside of programming to count from 1, and I imagine the reason for
this is simply that it's most useful in vast majority of real usage
people deal with.
It's just annoying that people bring up, for example, ground floor/
first floor as proof that both are equally natural. (I don't mean you, I
know you said you agree that 1-th indexing is more intuitive). -ak
More information about the Python-list