0

I have created my own class in a C Python extension. I want it to behave like a numpy array.

Let's say my class is a myarray. I can index it using the slice notation. This means that my implementation of the mp_subscript function of the mapping_methods looks correct. I can further index it, and it returns the element I want, of the correct type.

# Create a new myarray object of 10 elements
a = myarray( 10 )
# Get a slice of this myarray, as a numpy.ndarray object
#   returns new one created with PyArray_NewFromDescr (no copy)
b = a[2:4]
# What is the class of an indexed item in the slice?
print( b[0].__class__ )
<class 'numpy.int8'>

I also have implemented the direct index into my own type with a[0]. For this, I've tried calling PyArray_GETITEM. But the object I have in return is of type int.

# Create a new myarray object of 10 elements
a = myarray( 10 )
# What is the class of an indexed item in the slice?
#   returns the result of calling PyArray_GETITEM.
print( a[0].__class__ )
<class 'int'>

How can I, within my C extension, create this object of type numpy.int8?

Didier Trosset
  • 36,376
  • 13
  • 83
  • 122

1 Answers1

0

Found out I have to call PyArray_Scalar for this.

Didier Trosset
  • 36,376
  • 13
  • 83
  • 122