diff options
author | Raymond Hettinger <python@rcn.com> | 2010-09-15 00:12:05 +0000 |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2010-09-15 00:12:05 +0000 |
commit | 984ce40832609fabb04963518de593b0d17c4944 (patch) | |
tree | 9c50ccf006ef8fa026ccf1571b5d8606502690d6 /Doc/reference | |
parent | e5e75c64f24dc8c7e5ee17bfa46d7f3222ebc0fc (diff) | |
download | cpython-git-984ce40832609fabb04963518de593b0d17c4944.tar.gz |
Clarify where support for negative indices fall in the language hierarchy.
Diffstat (limited to 'Doc/reference')
-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 dff9cc3896..c7dd3fc6af 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 |