by goodside · 15y ago · 0 comments
It's extremely common to want to refer to some dynamic offset of a fixed location in memory, like if you have an array of equal-length items stored consecutively. If you have two literal addresses called Start and Index, you'd like to be able to say something like "Start[Index]" and have it mean "Read from Index, and whatever number is there, move that many spaces forward from Start, and give me that". It's handy that if Index is a cleared region of memory, you can say "Start[Index]" and have it mean the same thing as just "Start". More importantly, if "Start[Index]" only meant "Start" when Index was 1, that would imply that if Index were 0 that Start[Index] would refer to the spot right before Start, which is not only useless but outright dangerous.
But like many features of programming languages and operating systems (case-sensitivity, global menu bar, python's colon...), zero-based indexing is something that made perfect sense in the context and time in which it was originally designed and implemented, but it's really no longer necessary in most cases today, and only survives because of tradition and blind cargo cult-like copying of what others did before.
Traditions are not always arbitrary. Don't dismiss them until you're damned sure why they've survived, especially when you know for a fact that quantitative fields filled with high-order geniuses haven't bothered to scrap them. "Cargo-cult" thinking is a pernicious influence, but it's exceptionally rare that it's the complete explanation of anything.
myarray[2] doesn't mean 'myarray at time 2'
Since the latter are often used to approximate the former, this is quite handy.
See page 7 of this presentation: http://mvdirona.com/jrh/TalksAndPapers/GuidoVanRossum_21_yea...