[Python-Dev] PEP for adding an sq_index slot so that any object, a or b, can be used in X[a:b] notation (original) (raw)

Bengt Richter bokr at oz.net
Thu Feb 9 22:26:34 CET 2006


On Thu, 09 Feb 2006 01:00:22 -0700, Travis Oliphant <oliphant.travis at ieee.org> wrote:

Abstract This PEP proposes adding an sqindex slot in PySequenceMethods and an index special method so that arbitrary objects can be used in slice syntax. Rationale Currently integers and long integers play a special role in slice notation in that they are the only objects allowed in slice syntax. In other words, if X is an object implementing the sequence protocol, then X[obj1:obj2] is only valid if obj1 and obj2 are both integers or long integers. There is no way for obj1 and obj2 to tell Python that they could be reasonably used as indexes into a sequence. This is an unnecessary limitation. In NumPy, for example, there are 8 different integer scalars corresponding to unsigned and signed integers of 8, 16, 32, and 64 bits. These type-objects could reasonably be used as indexes into a sequence if there were some way for their typeobjects to tell Python what integer value to use. Proposal Add a sqindex slot to PySequenceMethods, and a corresponding index special method. Objects could define a function to place in the sqindex slot that returns an C-integer for use in PySequenceGetSlice, PySequenceSetSlice, and PySequenceDelSlice. How about if SLICE byte code interpretation would try to call obj.int() if passed a non-(int,long) obj ? Would that cover your use case?

BTW the slice type happily accepts anything for start:stop:step I believe, and something[slice(whatever)] will call something.getitem with the slice instance, though this is neither a fast nor nicely spelled way to customize.

Regards, Bengt Richter



More information about the Python-Dev mailing list