Message286098
I've failed to find where the behavior for negative indices in s[i:j]
expression (i, j < -len(s)) for standard sequences (str, list, etc) is
formally defined.
The observed behavior implemented in PySlice_GetIndicesEx(): If "len(s)
+ i" or "len(s) + j" is negative, use 0. [1] I don't see it in the docs.
if (*start < 0) *start += length;
if (*start < 0) *start = (*step < 0) ? -1 : 0;
...
if (*stop < 0) *stop += length;
if (*stop < 0) *stop = (*step < 0) ? -1 : 0;
The tutorial mentions [2]:
> out of range slice indexes are handled gracefully when used for
> slicing"
slice.indices() documentation says [3]:
> Missing or out-of-bounds indices are handled in a manner consistent
> with regular slices.
Neither define it explicitly.
The behavior for the upper boundary is defined explicitly [4]:
> If *i* or *j* is greater than ``len(s)``, use ``len(s)``
I've added the documentation patch that defines the behavior for the
lower boundary too.
[1] Objects/sliceobject.c
[2] Doc/tutorial/introduction.rst
[3] Doc/reference/datamodel.rst
[4] Doc/library/stdtypes.rst |
|
Date |
User |
Action |
Args |
2017-01-23 16:49:16 | akira | set | recipients:
+ akira, docs@python |
2017-01-23 16:49:16 | akira | set | messageid: <1485190156.24.0.631194950251.issue29352@psf.upfronthosting.co.za> |
2017-01-23 16:49:16 | akira | link | issue29352 messages |
2017-01-23 16:49:16 | akira | create | |
|