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 <oliphant at ee.byu.edu>
Status: Draft
Type:  Standards Track
Created:  09-Feb-2006
Python-Version:  2.5

Abstract

   This PEP proposes adding an nb_as_index 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 nb_index slot to PyNumberMethods, and a corresponding
   __index__ special method.  Objects could define a function to
   place in the sq_index slot that returns an appropriate
   C-integer for use as ilow or ihigh in PySequence_GetSlice, 
   PySequence_SetSlice, and PySequence_DelSlice.

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 _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
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to