Possible improvement to slice opperations.

Bengt Richter bokr at oz.net
Mon Sep 5 16:48:58 EDT 2005


On Mon, 5 Sep 2005 18:09:51 +0200, "Fredrik Lundh" <fredrik at pythonware.com> wrote:

>Steve Holden wrote:
>
>> Yes, I've been surprised how this thread has gone on and on.
>
>it's of course a variation of
>
>    "You can lead an idiot to idioms, but you can't make him
>    think ;-)"
>
>as long as you have people that insist that their original misunderstandings
>are the only correct way to model the real world, and that all observed
>inconsistencies in their models are caused by bugs in the real world, you'll
>end up with threads like this.
>
OTOH, ISTM we must be careful not to label an alternate alpha-version
"way to model the real world" as a "misunderstanding" just because it is alpha,
and bugs are apparent ;-)

BTW, it just occurred to me that alternate slice member semantics could be RELATIVE,
EACH depending on sign. I.e.,

    x[start:stop:step]

could mean start with x[start] as now as a starting point reference point element,
then let stop be a relative range length in either direction from the
starting point, and step absolute value indicating step size, and its sign
indicating insertion side for zero length slices. In effect, stop would
be a count for the slice range in either direction starting with the starting
element

    s = 'abcde'
    s[2:2]     => 'cd'
    s[2:-2]    => 'cb'
    s[-2:-3]   => 'dcb'
    s[-2:0]    => ''
    s[2:0]     => ''
    s[-2:-3:2] => 'db'
    r = range(10)
    r[5:0] = 'a'
    r => [0, 1, 2, 3, 4, 5, 'a', 6, 7, 8, 9]
    r[-2:0:-1] = 'b'
    r => [0, 1, 2, 3, 4, 5, 'a', 6, 7, 'b', 8, 9]
    r[-2:0] = ['c', 'd']
    r => [0, 1, 2, 3, 4, 5, 'a', 6, 7, 'b', 8, c, d, 9]

note that slice assignment would work by iterating through the right hand
sequence, which could do interesting things:

    r = range(6)
    r[3:-2] = 'ab'
    r => [0, 1, 'b', 'a', 4, 5]
but two reverse relative slices match order, so
    r = range(10)
    r[5:-3] = range(10)[-1:-3]  # rh seq is 9, 8
    r => [0, 1, 8, 9, 4, 5]

I think this is kind of interesting, and I probably wouldn't have thought of
it if I had not been for Ron's enthusiasm for his "misunderstanding" ;-)

In a way, "misunderstandings" are the mutations of open source evolution of ideas,
most of which die, but some of which mutate again and may occasionally survive.
So we need them ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list