[Numpy-discussion] how to work with numpy.int8 in c

David Cournapeau david at silveregg.co.jp
Tue Mar 2 20:51:04 EST 2010

James Bergstra wrote:

> Maybe I'm missing something... but I don't think I want to create an array.

Ah, I misunderstood your question. There are at least two ways:

PyObject *ret;
ret = PyArrayScalar_New(UInt8);


PyObject *ret; 

PyArray_Descr *typecode;

typecode = PyArray_DescrFromType(PyArray_UINT8); 

ret = PyArray_Scalar(NULL, typecode, NULL);

One way to set data in it is:

data = PyMem_Malloc(1);
*data = 4;
((PyArrayObject*)ret)->data = data;

Or simpler, but may be less flexible depending on what you are doing:

PyArrayScalar_VAL(ret, UInt8) = 4;

This should be documented better somewhere,



More information about the NumPy-Discussion mailing list