Add PEP 357, Allowing Any Object to be Used for Slicing (by Travis Oliphant).

This commit is contained in:
Guido van Rossum 2006-02-09 19:09:50 +00:00
parent 6b9a28d9c2
commit 4ab4d33cc9
2 changed files with 73 additions and 0 deletions

View File

@ -109,6 +109,7 @@ Index by Category
S 353 Using ssize_t as the index type von Loewis S 353 Using ssize_t as the index type von Loewis
S 354 Enumerations in Python Finney S 354 Enumerations in Python Finney
S 355 Path - Object oriented filesystem paths Lindqvist S 355 Path - Object oriented filesystem paths Lindqvist
S 357 Allowing Any Object to be Used for Slicing Oliphant
S 754 IEEE 754 Floating Point Special Values Warnes S 754 IEEE 754 Floating Point Special Values Warnes
Finished PEPs (done, implemented in Subversion) Finished PEPs (done, implemented in Subversion)
@ -408,6 +409,7 @@ Numerical Index
S 354 Enumerations in Python Finney S 354 Enumerations in Python Finney
S 355 Path - Object oriented filesystem paths Lindqvist S 355 Path - Object oriented filesystem paths Lindqvist
I 356 Python 2.5 Release Schedule Norwitz, et al I 356 Python 2.5 Release Schedule Norwitz, et al
S 357 Allowing Any Object to be Used for Slicing Oliphant
SR 666 Reject Foolish Indentation Creighton SR 666 Reject Foolish Indentation Creighton
S 754 IEEE 754 Floating Point Special Values Warnes S 754 IEEE 754 Floating Point Special Values Warnes
I 3000 Python 3.0 Plans Kuchling, Cannon I 3000 Python 3.0 Plans Kuchling, Cannon

71
pep-0357.txt Normal file
View File

@ -0,0 +1,71 @@
PEP: 357
Title: Allowing Any Object to be Used for Slicing
Version: $Revision$
Last Modified: $Date$
Author: Travis Oliphant <oliphant@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