[Python-Dev] [Python-checkins] r84823 - python/branches/py3k/Doc/reference/expressions.rst

Nick Coghlan ncoghlan at gmail.com
Wed Sep 15 15:28:20 CEST 2010

On Wed, Sep 15, 2010 at 10:09 AM, raymond.hettinger
<python-checkins at python.org> wrote:
> +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.

Perhaps mention the slice.indices(len) helper for performing the
standard conversion from negative indices to positive ones when
dealing with negative indices *in* slices?


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-Dev mailing list