Slice objects with negative increment
Alex Martelli
aleax at aleax.it
Thu May 2 11:18:50 EDT 2002
Paul Hughett wrote:
> Alex Martelli <aleax at aleax.it> wrote:
>
> : Paul Hughett wrote:
>
> : Why ever would it be? slice(j-L-1, i-L-1, -1) works per your stated
> : constraints and requirements for a sequence of length L, obviously.
>
> Ah, ha! It wasn't obvious to me that i-L-1 is an alias for i-1. And
Isn't that the whole point of "negative indices alias to nonnegative
ones"? It means that, for L>x>=0, x-L and x index the same item.
> it still seems bizarre to have to write that when I really mean i-1.
Unfortunately you don't _really_ mean that -- given said aliasing --
if the subtraction makes i from nonnegative to negative (i.e., if i
is exactly 0, in this case; easily generalizes to i-K for any given
K, of course).
> someone else's code. Not that I can think of any solution, other than
> to explain this trick when slice objects are explained.
Explaining is surely good. I think slicing can usefully be introduced
before slice-objects -- the latter are a tad harder because more general,
as they exist without specifically being tied to a given sequence (and
thus in particular to a given sequence _length_, which influences the
negative/nonnegative mapping).
Alex
More information about the Python-list
mailing list