Range Operation pre-PEP

Delaney, Timothy tdelaney at avaya.com
Wed May 9 20:20:00 EDT 2001


> ":" notation is very overused by the PEP 204.
> (makes to much confusion).
> IMHO, it's the main reason for Guido to reject it.
> 
> 
> >One thing that comes to mind is the interpretation
> >of the endpoint. The syntax strongly suggests that the
> >endpoint is inclusive, as you propose. But this is
> >not the most useful meaning in Python.
> 
> The reason under Python incl-excl nature is that
> these are intermediate points, needed for slice-operations
> to be natural like in:
> 
> a[0:0] = [1,2,3]
> 
> 
> There is no reason to bring this into ".." notation, because
> its different from ":" even visuall and it is more naturally
> to use convenient incl-incl ranges.

Well, there would be a simple way to make ranges that conformed to both. Use
the mathematical notation for describing inclusive and exclusive bounds
(although it would be using '..' instead of ',').

If I remember correctly ...

[0..10] - range is 0 to 10
[0..10) - range is 0 to 9
(0..10] - range is 1 to 10
(0..10) - range is 1 to 9

There - everyone is happy, and an unadorned 0..10 is an error.

This has the advantage of looking more like a sequence as well.

It also has the disadvantage that it may not be immediately obvious to
someone what the range is - '(' and '[' aren't *that* dissimilar when right
near each other.

The other consideration is how this would work with steps. Perhaps
overloading ':' here would work, since 0..10 unadorned would be invalid ...

[0..10:3] - range is 0 to 10, step 3
(10..0:-2] - range is 9 to 0, step -2

Anyway, I'm sure there are lots of objections to this, but I'd like to hear
them :)

Tim Delaney




More information about the Python-list mailing list