diff options
author | Raymond Hettinger <python@rcn.com> | 2010-09-15 00:09:26 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2010-09-15 00:09:26 (GMT) |
commit | f77c1d6adbdd02b84e7f8431b4c7088563a7a838 (patch) | |
tree | 056ddc231b0941981e52f88bd101f83b48526005 /Doc | |
parent | 476a31ef1e188d30a04629616b30e89758850303 (diff) | |
download | cpython-f77c1d6adbdd02b84e7f8431b4c7088563a7a838.zip cpython-f77c1d6adbdd02b84e7f8431b4c7088563a7a838.tar.gz cpython-f77c1d6adbdd02b84e7f8431b4c7088563a7a838.tar.bz2 |
Clarify where support for negative indices fall in the language hierarchy.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/reference/expressions.rst | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index b61ec08..5fb8052 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -518,11 +518,18 @@ whose value is one of the keys of the mapping, and the subscription selects the value in the mapping that corresponds to that key. (The expression list is a tuple except if it has exactly one item.) -If the primary is a sequence, the expression (list) must evaluate to an integer. -If this value is negative, the length of the sequence is added to it (so that, -e.g., ``x[-1]`` selects the last item of ``x``.) The resulting value must be a -nonnegative integer less than the number of items in the sequence, and the -subscription selects the item whose index is that value (counting from zero). +If the primary is a sequence, the expression (list) must evaluate to an integer +or a slice (as discussed in the following section). + +The formal syntax makes no special provision for negative indices in +sequences; however, built-in sequences all provide a :meth:`__getitem__` +method that interprets negative indices by adding the length of the sequence +to the index (so that ``x[-1]`` selects the last item of ``x``). The +resulting value must be a nonnegative integer less than the number of items in +the sequence, and the subscription selects the item whose index is that value +(counting from zero). Since the support for negative indices and slicing +occurs in the object's :meth:`__getitem__` method, subclasses overriding +this method will need to explicitly add that support. .. index:: single: character |