Q's: pythonD and range(1,12)

J

John Savage

I've very new to python, and am currently toying with pythonD. Could
someone please explain the rationale behind python designers' thinking
in deciding the function "range(1,12)" should return the sequence 1 to
11 rather than the more intuitively-useful 1 to 12??

After downloading the substantial distribution .zip file, I'm intrigued
to find it includes 3 files of identical size and creation date, differing
apparently only in name (python, python2.4 and python24.exe) and each of
exactly 2,597,888 bytes. What possible purpose could be served by such
triplication that couldn't more efficiently be done by other means?

Naive but curious minds wish to know!
 
S

Scott David Daniels

John said:
Could someone please explain the rationale behind python designers' thinking
in deciding the function "range(1,12)" should return the sequence 1 to
11 rather than the more intuitively-useful 1 to 12??
Essentially, it has to do with the decision to have range(5) mean the
list [0, 1, 2, 3, 4] (which is five elements long). Any choice will
please some and offend others; this one in Python has no chance of
changing.
After downloading the substantial distribution .zip file, I'm intrigued
to find it includes 3 files of identical size and creation date, differing
apparently only in name (python, python2.4 and python24.exe) and each of
exactly 2,597,888 bytes. What possible purpose could be served by such
triplication that couldn't more efficiently be done by other means?

You obviously know what and where PythonD came from, but we don't.
 
R

Raymond Hettinger

John said:
Could
someone please explain the rationale behind python designers' thinking
in deciding the function "range(1,12)" should return the sequence 1 to
11 rather than the more intuitively-useful 1 to 12??

There are several ways to do this, closed intervals, half-open
intervals, zero indexed, indexed from one, etc. Each way has its own
strengths and weaknesses. Guido chose the one he liked best and
applied the concept consistently throughout the language (slicing,
etc). His choice has some nice properties such as len(range(n))==n and
range(0,i)+range(i,n)==range(n).

The downside of the half-open interval approach is that it can be
confusing when counting backwards: range(n-1, -1, -1). Accordingly,
the builtin reversed() function was introduced so you could write this
in a more intuitive and readable manner: list(reversed(range(n))).

Some of this is a matter of taste and a matter of what you're used to
using, so the answer to what is the "more intuitively-useful" varies
depending on who is answering the question.


Raymond
 
S

Steven D'Aprano

There are several ways to do this, closed intervals, half-open
intervals, zero indexed, indexed from one, etc. Each way has its own
strengths and weaknesses. Guido chose the one he liked best and
applied the concept consistently throughout the language (slicing,
etc). His choice has some nice properties such as len(range(n))==n and
range(0,i)+range(i,n)==range(n).

The downside of the half-open interval approach is that it can be
confusing when counting backwards: range(n-1, -1, -1). Accordingly,
the builtin reversed() function was introduced so you could write this
in a more intuitive and readable manner: list(reversed(range(n))).

Some of this is a matter of taste and a matter of what you're used to
using, so the answer to what is the "more intuitively-useful" varies
depending on who is answering the question.

It is true that *some* of this is just a matter of taste, but more
importantly, *much* of this is a matter of objective superiority.

See, for example:

http://lists.canonical.org/pipermail/kragen-tol/2004-March/000757.html
http://www.jaggersoft.com/pubs/HowToWriteALoop.htm#Half-Open Interval

This thread discusses why a closed interval is better:

http://mail.python.org/pipermail/edu-sig/2004-April/003747.html

As near as I can tell from reading it, the sole reason a closed interval
is better is that in common English phrases "x to y" usually (but not
always) means the closed interval including both x and y.

A very common error in programming is the so-called fencepost error.
Half-open intervals help avoid them, while closed intervals tend to
result in this bug.

http://en.wikipedia.org/wiki/Fencepost_error

In other words, while the choice of half-open intervals is partly a matter
of taste, Guido is no dummy. Most of the time, his taste is influenced by
practical matters, and this is one of those times. Half-open intervals
will naturally help you avoid bugs.
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top