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

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


On 2006年2月09日 01:00:22 -0700, Travis Oliphant <oliphant.travis at ieee.org> wrote:
>>Abstract
>> This PEP proposes adding an sq_index 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 sq_index slot to PySequenceMethods, and a corresponding
> __index__ special method. Objects could define a function to
> place in the sq_index slot that returns an C-integer for use in
> PySequence_GetSlice, PySequence_SetSlice, and PySequence_DelSlice.
>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

AltStyle によって変換されたページ (->オリジナル) /