Possible improvement to slice opperations.

Bengt Richter bokr at oz.net
Wed Sep 7 02:10:40 EDT 2005


On Tue, 06 Sep 2005 10:31:33 GMT, Ron Adam <rrr at ronadam.com> wrote:
>Steve Holden wrote:
[...]
>
>> My point was that you can make those changes in your own code, leaving 
>> others to accept the situation as it is.
>
>It's only a suggestion and an interesting idea I thought I would share 
>and see if anyone would like to discuss. I did title this as a 'possible 
>improvement', and not as proposed improvement.
>
>
>> Right, I wasn't trying to suggest that you didn't know what you were 
>> talking about - or even that you didn't understand general relativity 
>> (on which my grasp could be said to be tenuous) - merely that some 
>> things are inherently difficult, and no matter how you twist the 
>> implementations about, the difficulties will remain.
>
>But shouldn't we try to make things easier when possible?
>
Sure ;-)

It occurs to me that maybe this discussion of slicing has a parallel
in mathematical intervals, and we might usefully check if it makes sense there.

IOW, let's compare [a, b] vs [a, b) vs (a, b] vs (a,b)

ISTM the python range model corresponds to [a, b) in terms of integers. The step thing
modifies that, but leave that aside to concetrate on the main issue.

For math, I think the notation requires a<=b, but for programming, python has a convention
for specifying related intervals and a subsetting function, with similar notation adding step.

Leaving aside abs(step)!=1 which specifies subsetting, we could say that

     [a:b:1]
is
     [a, b)
and
     [a:b,-1]
is
     (a, b]

but the latter returned in reverse order.

If we factor out the issues of reversing and subsetting, we just have
the indication of which kind of interval: half-open to the right or left.

That we could do by
    [a:b] => [a, b)
and
    .[a:b] => (a, b]

Then the question is, do we need sugar for reversed(x.[a:b])
or list(reversed(x.[a:b])) for the right hand side of a statement,
and do we want to to use both kinds of intervals in slice assignment?
(maybe and yes ;-)

The reason for yes is that it solves the which-gap problem in assigning to [a:a]
if you define [a, a) as an empty interval to the left of a and (a, a] as an empty
interval to the right of a, sort of like +0 and -0 in half-open intervals ;-)
Replacing the empty interval does the insertion on the side you want ;-)

I am choosing the convention to stay compatible with python's current behaviour,
even though I'm not sure what the math world says about a<x<=a vs a<=x<a as
different-in-some-sense intervals. I guess the intervals as point sets are the same,
but the interval definitions are different...

Other than the a:a distinction, in terms of integers and UIAM
    .[a:b]
is just sugar for
    [a+1:b+1]
but the sugar is nice, and the slice assignment to either side is nicer.

I'll deal with the subsetting another time ...

Regards,
Bengt Richter



More information about the Python-list mailing list