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. -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/
[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
On Nov 15, 6:50 pm, a...@pythoncraft.com (Aahz) wrote:
> 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. > -- > Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/
> [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.
On Sun, 15 Nov 2009 10:50:43 -0800, Aahz wrote: > 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)).
On Nov 15, 6:50 pm, a...@pythoncraft.com (Aahz) wrote:
> 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.
On Nov 15, 12:11 pm, Mark Dickinson <dicki...@gmail.com> wrote:
> On Nov 15, 6:50 pm, a...@pythoncraft.com (Aahz) wrote:
> > 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.
> Sounds like an excuse to post this Dijkstra link:
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.
In article <667394cb-d505-4906-8c6b-ab2d361b3...@j24g2000yqa.googlegroups.com>, Mark Dickinson <dicki...@gmail.com> wrote:
>On Nov 15, 6:50=A0pm, a...@pythoncraft.com (Aahz) wrote:
>> Anyone remember or know why Python slices function like half-open >> intervals? =A0I find it incredibly convenient myself, but an acquaintance >> familiar with other programming languages thinks it's bizarre and I'm >> wondering how it happened.
>Sounds like an excuse to post this Dijkstra link:
"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