number ranges

Alex Martelli aleaxit at yahoo.com
Mon Feb 20 11:59:22 EST 2006


John Zenger <john_zenger at yahoo.com> wrote:
   ...
> > for i in 2 to 5:
> >     print i,
> > 
> > which would print 2 3 4 5
> 
> This proposed syntax is also easy to understand, maybe more than "..",
> because it uses natural language.  The only criticism is that it creates
> a list without using [] notation.  How about [2 to 5]?

Having this hypothetical notation create a list (rather than a genexp)
would qualify as a blooper of the first magnitude. Historically, 'range'
was introduced before anything but a list was an option, so it had to be
grandfathered in (and doesn't use any [] either, BTW), but eventually
(in 3.0) it will return an iterator (as will methods keys/values/items
of dicts, and so forth).

If such notation was ever to appear in Python (and it won't, in part for
reasons I expand on below), it would surely be '(2 to 5)' and return an
iterator equivalent to what you could get today as iter(xrange(2,5)).

> > (I'm open to arguments that it should be more Pythonic and less 
> > mathematical, and halt at 4.)
> 
> I am also open to such arguments but it will be tough to convince me 
> that "x to y" should mean something different from what it means in 
> Pascal, BASIC, and English.

Andrew Koenig, in "C traps and pitfalls" (a great book), did a great job
explaining why semi-open intervals (lower bound included, upper bound
excluded) are vastly preferable. The common sloppy practice of including
both bounds leads to frequent "off by one" errors, also known as
"fencepost errors", which often occur in English too -- check out the
origin of the error's name, e.g. at
<http://catb.org/~esr/jargon/html/F/fencepost-error.html> .

Natural language has many ambiguities and subtle error-inducing traps
(indeed, being able to say something which means different things to
different people may have survival value for the crafty user of
language, so it can be argued that these traps and ambiguities are
adaptive;-). No reason for programming languages, or other formal
languages, to follow natural language down this destructive path.

In any case, the point is crucial: since Python will never do something
as abysmal as introducing a closed interval rather than a semiopen one,
the same kind of people who now long for '2 to 5' (unlikely to have
pondered Koenig's arguments, etc) will never be happy anyway, so it's
much better to avoid introducing '2 to 5' at all.


Alex




More information about the Python-list mailing list