Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Slicing history?

1 view
Skip to first unread message

Aahz

unread,
Nov 15, 2009, 1:50:43 PM11/15/09
to
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 (aa...@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

Jon Clements

unread,
Nov 15, 2009, 2:01:49 PM11/15/09
to
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.

Jon.

Nobody

unread,
Nov 15, 2009, 2:19:01 PM11/15/09
to
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)).

Mark Dickinson

unread,
Nov 15, 2009, 3:11:40 PM11/15/09
to
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:

http://www.cs.utexas.edu/~EWD/ewd08xx/EWD831.PDF

--
Mark

Steve Howell

unread,
Nov 15, 2009, 3:40:44 PM11/15/09
to

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.


Aahz

unread,
Nov 15, 2009, 5:06:47 PM11/15/09
to
In article <667394cb-d505-4906...@j24g2000yqa.googlegroups.com>,
Mark Dickinson <dick...@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:
>
>http://www.cs.utexas.edu/~EWD/ewd08xx/EWD831.PDF

Many thanks!

"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

0 new messages