Slicing history?

A

Aahz

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 ([email protected]) <*> 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
 
J

Jon Clements

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 ([email protected])           <*>        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.
 
N

Nobody

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)).
 
S

Steve Howell

Sounds like an excuse to post this Dijkstra link:

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top