From f33ff0d956c8b2f9e696acf10f1144c297b6519a Mon Sep 17 00:00:00 2001 From: Alex-Wasowicz <107991793+Alex-Wasowicz@users.noreply.github.com> Date: Tue, 16 Sep 2025 17:12:08 +0200 Subject: [PATCH 1/9] "Most" not "Some" seq. do implement negative index. --- Doc/reference/datamodel.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index da04cfde3bd587..c06452ca297c03 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -308,7 +308,7 @@ Sequences These represent finite ordered sets indexed by non-negative numbers. The built-in function :func:`len` returns the number of items of a sequence. When the length of a sequence is *n*, the index set contains the numbers 0, 1, -..., *n*-1. Item *i* of sequence *a* is selected by ``a[i]``. Some sequences, +..., *n*-1. Item *i* of sequence *a* is selected by ``a[i]``. Most sequences, including built-in sequences, interpret negative subscripts by adding the sequence length. For example, ``a[-2]`` equals ``a[n-2]``, the second to last item of sequence a with length ``n``. From 2d6768879186860c1eac0cdaaea8d05b44f72323 Mon Sep 17 00:00:00 2001 From: Alex-Wasowicz <107991793+Alex-Wasowicz@users.noreply.github.com> Date: Tue, 16 Sep 2025 17:23:28 +0200 Subject: [PATCH 2/9] Simplify example for negative index. --- Doc/reference/datamodel.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index c06452ca297c03..121f17cb61d790 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -310,8 +310,7 @@ built-in function :func:`len` returns the number of items of a sequence. When the length of a sequence is *n*, the index set contains the numbers 0, 1, ..., *n*-1. Item *i* of sequence *a* is selected by ``a[i]``. Most sequences, including built-in sequences, interpret negative subscripts by adding the -sequence length. For example, ``a[-2]`` equals ``a[n-2]``, the second to last -item of sequence a with length ``n``. +sequence length. For example, ``a[-1]`` is the last item (``a[n-1]``). .. index:: single: slicing From d242baff2ea69d72ac732897ca730cf07d9a02b7 Mon Sep 17 00:00:00 2001 From: Alex-Wasowicz <107991793+Alex-Wasowicz@users.noreply.github.com> Date: Tue, 16 Sep 2025 17:31:05 +0200 Subject: [PATCH 3/9] Length is added to negative index only once. --- Doc/reference/datamodel.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 121f17cb61d790..c13f058efb36fc 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -310,7 +310,8 @@ built-in function :func:`len` returns the number of items of a sequence. When the length of a sequence is *n*, the index set contains the numbers 0, 1, ..., *n*-1. Item *i* of sequence *a* is selected by ``a[i]``. Most sequences, including built-in sequences, interpret negative subscripts by adding the -sequence length. For example, ``a[-1]`` is the last item (``a[n-1]``). +sequence length (once). For example, ``a[-1]`` is the last item (``a[n-1]``), +but ``a[-n-1]`` doesn't exit. .. index:: single: slicing From 68aa560892501778c6b6dc7d4d92d6a5af189cf5 Mon Sep 17 00:00:00 2001 From: Alex-Wasowicz <107991793+Alex-Wasowicz@users.noreply.github.com> Date: Tue, 16 Sep 2025 17:49:48 +0200 Subject: [PATCH 4/9] Slice variables naming consistency. First index is *i*, then *k*, and then *x*, why? --- Doc/reference/datamodel.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index c13f058efb36fc..e3460ccd0912a7 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -315,14 +315,14 @@ but ``a[-n-1]`` doesn't exit. .. index:: single: slicing -Sequences also support slicing: ``a[i:j]`` selects all items with index *k* such -that *i* ``<=`` *k* ``<`` *j*. When used as an expression, a slice is a +Sequences also support slicing: ``a[start:stop]`` selects all items with index +*i* such that ``start <= i < stop``. When used as an expression, a slice is a sequence of the same type. The comment above about negative indexes also applies to negative slice positions. -Some sequences also support "extended slicing" with a third "step" parameter: -``a[i:j:k]`` selects all items of *a* with index *x* where ``x = i + n*k``, *n* -``>=`` ``0`` and *i* ``<=`` *x* ``<`` *j*. +Some sequences also support "extended slicing" with a third *step* parameter: +``a[start:stop:step]`` selects all items of *a* with index *i* where +``i = start + n*step``, ``n >= 0`` and ``start <= i < stop``. Sequences are distinguished according to their mutability: From c2f19ad8506e8ce1fb95a7b1e82b80c8e3581f48 Mon Sep 17 00:00:00 2001 From: Alex-Wasowicz <107991793+Alex-Wasowicz@users.noreply.github.com> Date: Tue, 23 Sep 2025 17:10:13 +0200 Subject: [PATCH 5/9] Describing negative step in slicing. --- Doc/reference/datamodel.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index e3460ccd0912a7..bb3f548740ef71 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -321,8 +321,9 @@ sequence of the same type. The comment above about negative indexes also applies to negative slice positions. Some sequences also support "extended slicing" with a third *step* parameter: -``a[start:stop:step]`` selects all items of *a* with index *i* where -``i = start + n*step``, ``n >= 0`` and ``start <= i < stop``. +``a[start:stop:step]`` selects all items of *a* with index *i*, where +``i = start + n*step`` and ``0 <= n``, such that ``start <= i < stop`` for +``0 <= step``, and ``stop < i <= start`` for ``step < 0``. Sequences are distinguished according to their mutability: From feec759a9cc1915a8f6cd0495aa03577cc3799bb Mon Sep 17 00:00:00 2001 From: Alex-Wasowicz <107991793+Alex-Wasowicz@users.noreply.github.com> Date: Tue, 23 Sep 2025 17:50:57 +0200 Subject: [PATCH 6/9] Describe omitted slice parameters. --- Doc/reference/datamodel.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index bb3f548740ef71..f98cff7f8bab10 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -325,6 +325,12 @@ Some sequences also support "extended slicing" with a third *step* parameter: ``i = start + n*step`` and ``0 <= n``, such that ``start <= i < stop`` for ``0 <= step``, and ``stop < i <= start`` for ``step < 0``. +Slicing parameters can be omitted: +* When *step* is omitted, it defaults to 1. +* When *start* is omitted, the selection starts from the 1st index depending on +the *step* direction (index 0 for ``0 < step``, subscript -1 for ``step < 0``). +* When *end* is omitted, the selection ends when there isn't any element left. + Sequences are distinguished according to their mutability: From ef4aff1be2fc7186ff277e95d130abe7a3a8dd53 Mon Sep 17 00:00:00 2001 From: Alex-Wasowicz <107991793+Alex-Wasowicz@users.noreply.github.com> Date: Wed, 24 Sep 2025 18:13:48 +0200 Subject: [PATCH 7/9] Fix formatting of previous commit. --- Doc/reference/datamodel.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index f98cff7f8bab10..71dba3e3bbec98 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -326,9 +326,11 @@ Some sequences also support "extended slicing" with a third *step* parameter: ``0 <= step``, and ``stop < i <= start`` for ``step < 0``. Slicing parameters can be omitted: + * When *step* is omitted, it defaults to 1. * When *start* is omitted, the selection starts from the 1st index depending on -the *step* direction (index 0 for ``0 < step``, subscript -1 for ``step < 0``). + the *step* direction (index 0 for ``0 <= step``, subscript -1 for + ``step < 0``). * When *end* is omitted, the selection ends when there isn't any element left. Sequences are distinguished according to their mutability: From d4cc863e9cf2e4c3f6fd188191891a128efa1778 Mon Sep 17 00:00:00 2001 From: Alex-Wasowicz <107991793+Alex-Wasowicz@users.noreply.github.com> Date: Tue, 7 Oct 2025 09:50:13 +0200 Subject: [PATCH 8/9] Seq slicing desc. now allows 3rd party views By previous description, NumPy's `ndarray` wasn't a sequence --- Doc/reference/datamodel.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 71dba3e3bbec98..63deb6e82f3fe4 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -316,8 +316,9 @@ but ``a[-n-1]`` doesn't exit. .. index:: single: slicing Sequences also support slicing: ``a[start:stop]`` selects all items with index -*i* such that ``start <= i < stop``. When used as an expression, a slice is a -sequence of the same type. The comment above about negative indexes also applies +*i* such that ``start <= i < stop``. When used as an expression, a slice should be +a sequence of the same type, but must be a sequence behaving as one. +The comment above about negative indexes also applies to negative slice positions. Some sequences also support "extended slicing" with a third *step* parameter: From 1e26e92dde35eb9bba15d221234c3c97e3310f1b Mon Sep 17 00:00:00 2001 From: Alex-Wasowicz <107991793+Alex-Wasowicz@users.noreply.github.com> Date: Thu, 6 Nov 2025 12:15:24 +0100 Subject: [PATCH 9/9] Removed single trailing whitespace. --- Doc/reference/datamodel.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst index 63deb6e82f3fe4..37c012e13c04a5 100644 --- a/Doc/reference/datamodel.rst +++ b/Doc/reference/datamodel.rst @@ -323,7 +323,7 @@ to negative slice positions. Some sequences also support "extended slicing" with a third *step* parameter: ``a[start:stop:step]`` selects all items of *a* with index *i*, where -``i = start + n*step`` and ``0 <= n``, such that ``start <= i < stop`` for +``i = start + n*step`` and ``0 <= n``, such that ``start <= i < stop`` for ``0 <= step``, and ``stop < i <= start`` for ``step < 0``. Slicing parameters can be omitted: