[Python-checkins] r42549 - peps/trunk/pep-0357.txt
guido.van.rossum
python-checkins at python.org
Wed Feb 22 05:00:20 CET 2006
Author: guido.van.rossum
Date: Wed Feb 22 05:00:18 2006
New Revision: 42549
Modified:
peps/trunk/pep-0357.txt
Log:
Travis's changes.
Modified: peps/trunk/pep-0357.txt
==============================================================================
--- peps/trunk/pep-0357.txt (original)
+++ peps/trunk/pep-0357.txt Wed Feb 22 05:00:18 2006
@@ -12,7 +12,7 @@
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 @@
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 @@
7) Add the operator.index(x) function.
-Possible Concerns
+Discussion Questions
Speed:
@@ -109,7 +107,7 @@
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 @@
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 @@
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
More information about the Python-checkins
mailing list