New version by Travis. Adds rationale for __index__ name.

Reformats some.
This commit is contained in:
Guido van Rossum 2006-02-13 18:18:57 +00:00
parent e73718f969
commit 8854c320fa
1 changed files with 30 additions and 16 deletions

View File

@ -17,13 +17,13 @@ Abstract
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.
Currently integers and long integers play a special role in
slicing 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
@ -33,18 +33,22 @@ Rationale
an integer.
It is not possible to use the nb_int (and __int__ special method)
for this purpose because that method is used to *coerce* objects to
integers. It would be inappropriate to allow every object that can
be coerced to an integer to be used as an integer everywhere Python
expects a true integer.
for this purpose because that method is used to *coerce* objects
to integers. It would be inappropriate to allow every object that
can be coerced to an integer to be used as an integer everywhere
Python expects a true integer. For example, if __int__ were used
to convert an object to an integer in slicing, then float objects
would be allowed in slicing and x[3.2:5.8] would not raise an error
as it should.
Proposal
Add a nb_index slot to PyNumberMethods, and a corresponding
Add an nb_index slot to PyNumberMethods, and a corresponding
__index__ special method. Objects could define a function to place
in the nb_index slot that returns an appropriate C-integer for use
as ilow or ihigh in PySequence_GetSlice, PySequence_SetSlice, and
PySequence_DelSlice.
PySequence_DelSlice. This integer could also be used elsewhere by
Python when a C-integer is required.
Implementation Plan
@ -57,8 +61,9 @@ Implementation Plan
3) Change the _PyEval_SliceIndex function to accomodate objects
with the index slot defined.
4) Change all builtin objects that use the subscript form and
special-check for integers to check for the slot as well
4) Change all builtin objects (e.g. lists) that use the as_mapping
slots for subscript access and use a special-check for integers to
check for the slot as well.
5) Add PyNumber_Index C-API to return an integer from any
Python Object that has the nb_index slot.
@ -84,9 +89,18 @@ Possible Concerns
thing is that float objects already define the nb_int method, but
float objects *should not* be used as indexes in a sequence.
Why the name __index__?:
Some questions were raised regarding the name __index__ when other
interpretations of the slot are possible. For example, the slot
can be used any time Python requires an integer internally (such
as in "mystring" * 3). The name was suggested by Guido because
slicing syntax is the biggest reason for having such a slot and no
better name emerged.
Reference Implementation
Submitted as a patch to SourceForge.
Submitted as patch 1429591 to SourceForge.
Copyright