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: sequence slicing documentation incomplete
Type: enhancement Stage:
Components: Documentation Versions: Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: extended slice behavior inconsistent with docs
View: 1446619
Assigned To: Nosy List: ajaksu2, bethard, georg.brandl, rhettinger
Priority: normal Keywords:

Created on 2005-08-20 21:30 by bethard, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg26095 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2005-08-20 21:30
Here's what points (3) and (5) of the Sequence Types[1]
documentation say now:

"""
(3) If i or j is negative, the index is relative to the
end of the string: len(s) + i or len(s) + j is
substituted. But note that -0 is still 0.
...
(5) The slice of s from i to j with step k is defined
as the sequence of items with index x = i + n*k such
that $0 \leq n < \frac{j-i}{k}$. In other words, the
indices are i, i+k, i+2*k, i+3*k and so on, stopping
when j is reached (but never including j). If i or j 
is greater than len(s), use len(s). If i or j are
omitted then they become ``end'' values (which end
depends on the sign of k). Note, k cannot be zero.
"""

I'd like to replace that second-to-last sentence in
point (5), which uses the vague word "end'' in its
description, with something more explicit.  I'd like it
to read something like:

"""
If k is positive and i or j is omitted, they will be
replaced with 0 and len(s) respectively. If k is
negative and i or j is omitted, they will be replaced
with -1 and -len(s)-1 respectively.  Note that these
replacements happen before the rule from point (3) is
applied.
"""

I'd also like to put an example with point (5) to show
this behavior in action. Something like:

"""
So for example::

    >>> seq = 'abcde'
    >>> len(seq)
    5
    >>> 'edc' == seq[:1:-1] == seq[-1:1:-1]
    True
    >>> 'ca' == seq[2::-2] == seq[2:-5-1:-2]
    True

Note however that manually applying the rule from point
(3) (adding len(s) to any i or j that is negative)
works for i, but does not work for j::

    >>> seq[5-1:1:-1]
    'edc'
    >>> seq[2:-1:-2]
    ''

This is because Python sees the -1 in the j position
and applies the rule from point (3) again.
"""

One of the motivations for this patch is to be able to
explain the difference in behavior between sequence
slicing and slice.indices(), e.g.

    >>> seq[::-2]
    'eca'
    >>> slice(None, None, -2).indices(5)
    (4, -1, -2)
    >>> seq[4:-1:-2]
    ''

Right now, I think the documentation is too vague to be
able to make it clear why slices behave differently. 
If this patch is accepted, I'll work on a patch for
slice objects that explains the differences.

[1] http://docs.python.org/lib/typesseq.html 
msg26096 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-08-21 09:29
Logged In: YES 
user_id=80475

-0

The docs read better with "vague" words like "end".  Making
the description more mathematical may make it more precise
but will also make it harder to just read and understand. 
The wording suggested above is fathomable only after you
already understand how slicing works.  It is the "vague"
words that help achieve the understanding in the first place.

Also, I do not want to clutter this section with examples
intended to compare and contrast slices versus
slice.indices.  It would be okay to add a brief comparative
example to the docs for slice.indices().  Understanding the
latter is aided by a comparison.  In contrast, you can get
pretty good with slices and know nothing about
slice.indices() which is a somewhat challenging method.
msg26097 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2005-08-23 02:25
Logged In: YES 
user_id=945502

Sure, I'm not particular.  If you can put some of this in the 
slice.indices(), that would be great too.  I'd just like it (or 
something like it) to be somewhere, because there's currently 
no good place to reference for the differences between 
sequence slicing and slice.indices() behavior.
msg82193 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2009-02-16 00:27
Is this a won't fix, then?
msg85535 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-04-05 17:32
There's also #1446619, so closing as a duplicate.
History
Date User Action Args
2022-04-11 14:56:12adminsetgithub: 42294
2009-04-05 17:32:29georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg85535

superseder: extended slice behavior inconsistent with docs
resolution: duplicate
2009-02-16 00:27:08ajaksu2settype: enhancement
messages: + msg82193
nosy: + ajaksu2
versions: + Python 2.7, - Python 2.4
2005-08-20 21:30:18bethardcreate