Guido seemed accepting to this idea about 9 months ago when I spoke to
him. I finally got around to writing up the PEP. I'd really like to
get this into Python 2.5 if possible.
-Travis
PEP: ###
Title: Allowing any object to be used for slicing
Version: $Revision 1.1$
Last Modified: $Date: 2006/02/09 $
Author: Travis Oliphant <oliphant at ee.byu.edu>
Status: Draft
Type: Standards Track
Created: 09-Feb-2006
Python-Version: 2.5
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.
Implementation Plan
1) Add the slots
2) Change the ISINT macro in ceval.c to accomodate objects with the
index slot defined.
3) Change the _PyEval_SliceIndex function to accomodate objects
with the index slot defined.
Possible Concerns
Speed:
Implementation should not slow down Python because integers and long
integers used as indexes will complete in the same number of
instructions. The only change will be that what used to generate
an error will now be acceptable.
Why not use nb_int which is already there?
The nb_int, nb_oct, and nb_hex methods are used for coercion.
Floats have these methods defined and floats should not be used in
slice notation.
Reference Implementation
Available on PEP acceptance.
Copyright
This document is placed in the public domain
_______________________________________________
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com