[issue28882] RFC: Slice confusing with negative strides and the 0th element.

2016-12-06 Thread Josh Rosenberg

Josh Rosenberg added the comment:

I find this report nigh incomprehensible, but I will admit that I can't seem to 
find any good explanations of the extended slicing rules in the Python docs.

The tutorial covers slicing, but, AFAICT, it never mentions extended slicing at 
all, not in 3.1.2 or 3.1.3 (Strings and Lists introduction), nor anywhere in 
section 5 (Data structures). 3.1.2 and 3.1.3 even lie a little (claiming the 
implicit start is always 0, and the implicit end is always len(sequence), when 
those reverse for a negative slice step).

The slice object and the slice glossary entry doesn't cover it either, nor link 
to anything that does. The Data model entry for Slicings doesn't seem to 
describe meaning either.

--
nosy: +josh.r

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28882] RFC: Slice confusing with negative strides and the 0th element.

2016-12-05 Thread steven Michalske

steven Michalske added the comment:

Argh, I swear I proofread this...

print([a[x] for x in [slice(y+3, y-1, -1) for y in range(0, len(a), 4)]])
[[], [7, 7, 6, 5]]

Catching my explicit case, I changed my code to:
print([a[x] for x in [slice(y+3, y-1 if y > 1 else None, -1) for y in range(0, 
len(a), 4)]])
[[3, 2, 1, 0], [7, 6, 5, 4]]

Also, I could have done this...

print(list(reversed([a[x] for x in [slice(y, y-4, -1) for y in range(-1, 
-len(a), -4)]])))
[[3, 2, 1, 0], [7, 6, 5, 4]]
But, Yikes All those inverses!

Side Note
I wish we had partitioning in ranges/slices.

a[::4] == [0, 4]
a[::4:2] == [[0, 1], [4, 5]]
a[::4:-4] == [[3, 2, 1, 0], [7, 6, 5, 4]]

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28882] RFC: Slice confusing with negative strides and the 0th element.

2016-12-05 Thread steven Michalske

New submission from steven Michalske:

The slicing and using inputed math is is necessary to provide a special case to 
make the code behave as expected.

Unless I'm missing something.  Like we can't do this, as we loose negative 
indexing from the end of the file?

Let's take the following example, byte swapping 32bit integers.

a = [0,1,2,3,4,5,6,7]

print([a[x] for x in [slice(y+3, y-1 if y > 1 else None, -1) for y in range(0, 
len(a), 4)]])
[[], [7, 7, 6, 5]]

Catching my explicit case, I changed my code to:
print([a[x] for x in [slice(y+3, y-1 if y > 1 else None, -1) for y in range(0, 
len(a), 4)]])
[[3, 2, 1, 0], [7, 6, 5, 4]]

Life proceeds as I am explicit, but now I have a conditional check that is 
slowing me down...

It appears that -1 is being considered the last element in the set.
This was surprising, as I couldn't use simple math to byte swap, I needed to 
pass None instead of -1

It appears PySlice_GetIndices in file cpython/Objects/sliceobject.c always 
adds length if stop < 0 regardless to start and step.

if (r->stop == Py_None) {
*stop = *step < 0 ? -1 : length;
} else {
if (!PyLong_Check(r->stop)) return -1;
*stop = PyLong_AsSsize_t(r->stop);
if (*stop < 0) *stop += length;   # <-- Issue here?
}


It seems that there is some undocumented logic and behavioral decisions.

Was it explicitly decided that a negative stop and negative stride
e.g. 
In [46]: a[3:0:-1]
Out[46]: [3, 2, 1]

In [47]: a[3:-1:-1]
Out[47]: []

Not [3,2,1,0]  (My least surprising value...)

Because -1 is based on len(a).

I expected that with a positive start, and a negative stride that the -1 case 
would be considered include 0.

In other code...
[4:-1:-1] == [4:None:-1]
Not
[4:-1:-1] == [4:len(a)-1:-1]
Especially when len(a)-1 > start

I understand that this is behavioral, but it is confusing...

Discussion?

--
messages: 282500
nosy: hardkrash
priority: normal
severity: normal
status: open
title: RFC: Slice confusing with negative strides and the 0th element.
type: behavior
versions: Python 3.5

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com