[Python-Dev] Indexing builtin sequences with objects which supply __int__

Tim Peters tim.one@comcast.net
Fri, 21 Jun 2002 20:03:01 -0400


[Todd Miller, wants to use rank-0 arrays as regular old indices]

Here's a sick idea:  given Python 2.2, you *could* make the type of a rank-0
array a subclass of Python's int type, making sure (if needed) to copy the
value into "the int part" at the start of the struct.  Then a rank-0 array
would act like an integer in almost all contexts requiring a Python int,
including use as a sequence index.

The relevant code in the core is

		if (PyInt_Check(key))
			return PySequence_GetItem(o, PyInt_AsLong(key));

in PyObject_GetItem().  PyInt_Check() says "yup!" for an instance of any
subclass of int, and PyInt_AsLong() extracts "the int part" out of any
instance of any subclass of int.

In return, it shifts the burden onto convincing the rest of numarray that
the thing is still an array too <0.4 wink>.