[Python-ideas] [Python-Dev] Inclusive Range

Antoon Pardon Antoon.Pardon at rece.vub.ac.be
Wed Oct 6 07:14:30 EDT 2010


On Tue, Oct 05, 2010 at 01:52:39PM -0700, Chris Rebert wrote:
> On Tue, Oct 5, 2010 at 1:31 PM, Wolfgang Rohdewald
> <wolfgang at rohdewald.de> wrote:
> > On Dienstag 05 Oktober 2010, MRAB wrote:
> >> > About notation, even if loved right-hand-half-open
> >> > intervals, I would wonder about [a,b] noting it. I guess
> >> > 99.9% of programmers and novices (even purely amateur) have
> >> > learnt about intervals at school in math courses. Both
> >> > notations I know of use [a,b] for closed intervals, while
> >> > half-open ones are noted either [a,b[ or [a,b). Thus, for
> >> > me, the present C/python/etc notation is at best
> >> > misleading. So, what about a hypothetical language using
> >> > directly math unambiguous notation, thus also letting
> >> > programmers chose their preferred semantics (without
> >> > fooling others)? End of war?
> >>
> >> Dijkstra came to his conclusion after seeing the results of
> >> students using the programming language Mesa, which does
> >> support all 4 forms of interval.
> >
> > what was his conclusion?
> 
> That right-hand-half-open intervals (i.e. a <= i < b, equivalently [a,
> b) ), which are what Python uses, are to be preferred.
> (See aforelinked PDF: http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF)

The problem is that the slice notation is sometimes handy in situations where
an open interval doesn't allow easily to mark what you want.

For instance I have at one time implemted a Tree. This is a dict like structure
but it allows to visit the keys in order. Because there is an order, slice
notation can make sense. e.g. if T is a tree with names as keys, T['bea':'mike']
is a subtree where we have for each key that 'bea' <= key < 'mike'.

But what if I wanted a subtree where 'mike' was still included, but nothing further?
Or what if the keys were floats or tuples and I wanted an inclusive upper boundary?

And what if you needed the reverse sequence. If you start with inclusive limit,
the reverse of a <= item <= b is b >= item >= a. If the second limit is to be
exclusive the reverse of a <= item < b becomes (b - 1) >= item > (a - 1).

So what do you do in python, if you are given a list, a lower inclusive and an
upper exclusive limit and you have to return the reverse slice. Better don't
use L[b-1:a-1:-1], because that may result in a nasty surprise, a surprise that
could have been avoided by allowing the second limit to be inclusive.

So I don't think the results in MESA are that relevant when you are working
in an environment that is not limited to integers in upgoing sequences.

-- 
Antoon Pardon



More information about the Python-list mailing list