How Python works: What do you know about support for negative indices?

Raymond Hettinger python at rcn.com
Mon Sep 13 16:07:19 EDT 2010


On Sep 10, 2:13 pm, Terry Reedy <tjre... at udel.edu> wrote:

> Reading the third paragraph out of context, one can miss the restriction
> to built-in objects. I had assumed that the conversion using len(), when
> available, happened prior to the __getitem__ call.

Yes, that's a common misconception.  It is probably based on
the current wording of the docs and on the PySequence_GetItem()
optimized fast-path for builtin and extension sequences.

>From the users point-of-view, the important thing is that it
(effectively) occurs in the __getitem__() code, that builtin
sequences and extensions support it, and that else where it is
optional.

Another way of saying it is:  if you are ever writing a __getitem__()
method in Python (even for a subclass of a builtin sequence), then it
is up to you to decide whether to add support for negative indices
and slicing.


Raymond



More information about the Python-list mailing list