[Numpy-discussion] Subclassing ndarray in C: getitem ?

Pierre GM pgmdevlist at gmail.com
Wed Jul 28 18:43:30 EDT 2010


On Jul 28, 2010, at 5:52 PM, Travis Oliphant wrote:

> 
> On Jul 26, 2010, at 11:58 AM, Pierre GM wrote:
> 
>> All,
>> I'm teaching myself how to subclass ndarrays in C (not in Cython, just plain C). It's slowly coming together, but I'm now running into a problem: I need to overwrite __getitem__ and I'm not sure how to do it. I was thinking about using my own function instead of array_as_mapping.mp_subscript, but how do I import array_as_mapping in the first place ? And is it really the way to go ?
>> Any pointer appreciated...
> 
> To subtype in C, you have to populate the function pointer tables.  To "over-ride" __getitem__ you have to replace the function pointer, which in the case of mp_subscript is in the array_as_mapping table --- which you have to create as a static pointer table that contains the functions you want and then point the TypeObject structure to it some-time during initialization. 
> 
> I hope that helps a little bit.  

Mmh. I did create a PyMappingMethod structure called MyArray_as_mapping, and MyArray_as_mapping.mp_subscript points to the function that I want to use. However, I'd like the MyArray_as_mapping.length and MyArray.mp_ass_subscript to point to their PyArray equivalent. I was hoping to do something like 
MyArray_as_mapping.length = array_as_mapping.length
but I don't know how to import array_as_mapping.
I also tried to do 
MyArray_as_mapping.length = &PyArray_Type->tp_as_mapping->length
but it doesn't work... And that's where I'm stuck...




More information about the NumPy-Discussion mailing list