[on old computer technologies and programmers] "Fancy tail fins on a
brand new '59 Cadillac didn't mean throwing out a whole generation of
mechanics who started with model As." --Andrew Dalke
Good ol' zero based indexing. It makes a lot more sense that range(len
(my_list)) returns 'n' values which are valid indicies, otherwise
they'd be a lot of IndexError's being raised.
Besides, when you really want the full range (a corner case), it's a
lot easier to do a +1, than to force people to write -1 for the vast
majority of cases.
Jon.
> Anyone remember or know why Python slices function like half-open
> intervals? I find it incredibly convenient myself, but an acquaintance
> familiar with other programming languages thinks it's bizarre and I'm
> wondering how it happened.
How else would they function? Closed intervals?
Using a closed interval (for just about anything) indicates that the
designer has very limited programming experience. Anyone with a modicum of
programming experience knows that half-open intervals are the norm, and
that using closed intervals will confuse anyone else with a modicum of
programming experience.
That's aside from the objective merits, i.e. the fact that they can be
used to partition an interval into subintervals without having to
adjust the upper bound (which requires knowing how much to adjust the
upper bound by, if that's even possible (for reals, it isn't)).
Sounds like an excuse to post this Dijkstra link:
http://www.cs.utexas.edu/~EWD/ewd08xx/EWD831.PDF
--
Mark
That is really good stuff! Like Aahz I have Python's slicing
mechanism (and zero-based indexing) burnt into my brain, but I never
had a good way to explain why it makes sense, other than just an
intuitive notion that it works for me. It is interesting how the link
actually seems to explain zero-based indexing as a consequence of the
slicing approach, not a cause. I always understood zero-based
indexing as a relic of memory management, which was fine, but I guess
the reasons go deeper than that.
Many thanks!
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it." --Brian W. Kernighan