[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)
Brett Cannon brett at python.org
Thu Feb 9 23:32:47 CET 2006
- Previous message: [Python-Dev] PEP for adding an sq_index slot so that any object, a or b, can be used in X[a:b] notation
- Next message: [Python-Dev] PEP for adding an sq_index slot so that any object, a or b, can be used in X[a:b] notation
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Looks good to me. Only change I might make is mention why int doesn't work sooner (such as in the rationale). Otherwise +1 from me.
-Brett
On 2/9/06, Travis E. Oliphant <oliphant.travis at ieee.org> wrote:
Attached is an updated PEP for 357. I think the index concept is better situated in the PyNumberMethods structure. That way an object doesn't have to define the Sequence protocol just to be treated like an index. -Travis
PEP: 357357357 Title: Allowing any object to be used for slicing Version: Revision 1.2 Last Modified: 02/09/2006 Author: Travis Oliphant Status: Draft Type: Standards Track Created: 09-Feb-2006 Python-Version: 2.5 Abstract This PEP proposes adding an nbasindex slot in PyNumberMethods 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 nbindex slot to PyNumberMethods, and a corresponding index special method. Objects could define a function to place in the sqindex slot that returns an appropriate C-integer for use as ilow or ihigh in PySequenceGetSlice, PySequenceSetSlice, and PySequenceDelSlice. Implementation Plan 1) Add the slots 2) Change the ISINT macro in ceval.c to ISINDEX and alter it to accomodate objects with the index slot defined. 3) Change the PyEvalSliceIndex 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 nbint which is already there? The nbint, nboct, and nbhex 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 Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/brett%40python.org
- Previous message: [Python-Dev] PEP for adding an sq_index slot so that any object, a or b, can be used in X[a:b] notation
- Next message: [Python-Dev] PEP for adding an sq_index slot so that any object, a or b, can be used in X[a:b] notation
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]