This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Slicing (operation) is not symmetrical with respect to suffixes and prefixes
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.6
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: jksware, serhiy.storchaka
Priority: normal Keywords:

Created on 2016-10-01 18:57 by jksware, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg277829 - (view) Author: Juan Carlos Pujol Mainegra (jksware) Date: 2016-10-01 18:57
Let s be a string or other array-like object, a, b > 0 integers, s[0:b] returns a b-long prefix to s, the same as s[:b], but s[-a:0] returns empty (for len(s) > 0 and a > 1), while it should return the same as s[-a:], an a-long suffix (a > 0).

A syntax asymmetry like this shall not be imposed to those using non-literal slicing indexes, as it would be necessarily to introduce a control condition to test whether the upper bound index is a non-negative quantity, assuming the lower bound index is negative. Furthermore, it breaks the whole negative slicing idea, being that (I consider) index i always be treated as i mod len(s), so that constructions like s[-a:b] (for a, b > 0 or a, b < 0) could return s[-a:] + s[:b].
msg277831 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-10-01 19:52
The default value of the lower bound index is 0, but the default value of the upper bound index is the length of the sequence. s[:a] is the same as s[0:a], but s[a:] is the same as s[a:len(s)].

If make s[a:0] meaning the same as s[a:len(s)], i.e. return a subsequence from index a to the end, while s[0:a] means the same as s[:a], i.e. returns a subsequence from the begin to index a, what should mean s[0:0]? There is a contradiction.

This change would break existing code.
History
Date User Action Args
2022-04-11 14:58:37adminsetgithub: 72523
2016-10-01 19:52:42serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg277831

resolution: rejected
stage: resolved
2016-10-01 18:57:29jkswarecreate