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

Todd Miller jmiller@stsci.edu
Sat, 22 Jun 2002 21:22:52 -0400


Tim Peters wrote:

>[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
>
Right now, numarray is a subclass of object for Python-2.2 in order to 
get properties in order to emulate some of Numeric's attributes. I'm 
wondering what I'd loose from object in order to pick up int's indexing. 
 I'm also wondering how to make a rank-0 Float array fail as an index. 
 I might try it just to see where it breaks...  Thanks!

>
>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>.
>