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.

Author bethard
Recipients
Date 2005-08-20.21:30:18
SpamBayes Score
Marked as misclassified
Message-id
In-reply-to
Content
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 
History
Date User Action Args
2007-08-23 14:33:56adminlinkissue1265100 messages
2007-08-23 14:33:56admincreate