Travis's changes.

This commit is contained in:
Guido van Rossum 2006-02-22 04:00:18 +00:00
parent 04878ba988
commit b92889b332
1 changed files with 13 additions and 10 deletions

View File

@ -12,7 +12,7 @@ Abstract
This PEP proposes adding an nb_index slot in PyNumberMethods and an
__index__ special method so that arbitrary objects can be used
whenever only integers are called for in Python, such as in slice
whenever integers are explicitly needed in Python, such as in slice
syntax (from which the slot gets its name).
Rationale
@ -62,15 +62,13 @@ Specification:
def __index__(self):
return obj
Where obj must be either an int or a long or another object that has the
__index__ special method (but not self).
where obj must be either an int or a long.
3) A new C-API function PyNumber_Index will be added with signature
Py_ssize_t PyNumber_Index (PyObject *obj)
which will special-case integer and long integer objects but otherwise
return obj->ob_type->tp_as_number->nb_index(obj) if it is available.
which will return obj->ob_type->tp_as_number->nb_index(obj) if it is available.
A -1 will be returned and an exception set on an error.
4) A new operator.index(obj) function will be added that calls
@ -100,7 +98,7 @@ Implementation Plan
7) Add the operator.index(x) function.
Possible Concerns
Discussion Questions
Speed:
@ -109,7 +107,7 @@ Possible Concerns
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?:
Why not use nb_int which is already there?
The nb_int method is used for coercion and so means something
fundamentally different than what is requested here. This PEP
@ -119,7 +117,7 @@ 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__?:
Why the name __index__?
Some questions were raised regarding the name __index__ when other
interpretations of the slot are possible. For example, the slot
@ -139,10 +137,15 @@ Possible Concerns
makes sense to return the C-integer directly and not wrapped
in a Python int object.
Why can't __index__ return any object with the nb_index method?
This would allow infinite recursion in many different ways that are not
easy to check for. This restriction is similar to the requirement that
__nonzero__ return an int or a bool.
Reference Implementation
Submitted as patch 1429591 to SourceForge. This patch number does
not contain implementation 5.
Submitted as patch 1436368 to SourceForge.
Copyright