Python 如何在C numpy扩展中创建numpy.int8类型的对象?
我已经在C Python扩展中创建了自己的类。我希望它的行为像一个numpy数组 假设我的类是myarray。我可以使用切片表示法对其进行索引。这意味着我对Python 如何在C numpy扩展中创建numpy.int8类型的对象?,python,numpy,cpython,python-c-api,Python,Numpy,Cpython,Python C Api,我已经在C Python扩展中创建了自己的类。我希望它的行为像一个numpy数组 假设我的类是myarray。我可以使用切片表示法对其进行索引。这意味着我对mapping\u方法的mp\u subscript函数的实现看起来是正确的。我可以进一步索引它,它返回我想要的正确类型的元素 # Create a new myarray object of 10 elements a = myarray( 10 ) # Get a slice of this myarray, as a numpy.nda
mapping\u方法的mp\u subscript
函数的实现看起来是正确的。我可以进一步索引它,它返回我想要的正确类型的元素
# 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'>
如何在我的C扩展中创建这个类型为numpy.int8
的对象?发现我必须为此调用PyArray\u Scalar
# 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'>