How does a C API-function returns a list to python

YarLevub yarlevub at aol.com
Sun Jan 12 16:11:33 EST 2003


>double a[5]={1.0,2.0,3.0,4.0,5.0};
>....
>return Py_BuildValue("???",a)
>....

The simplest way is to return a string containing the data and then use the
array module to make it available to your Python program.

return Py_BuildValue("s#", a, sizeof(a));

in your Python program

from array import array
...

s = myfunction(...)

a = array('d', s)

Now you can index a to get at the data.

There are of course many other ways to solve this problem but I do this
frequently.

Ray Buvel





More information about the Python-list mailing list