How to use a Python function that returns a double array in C++.

P

pieter

I want to use a Python function that returns a double array in C++.

I don’t have a problem if the Python function returns a single
variable of type double, using the following lines in my C++ code:
myPythonObjectPointer = PyObject_CallObject(pFunc, pArgs);
myCppDoubleVariable = PyFloat_AsDouble(myPyObjectPointer);
(I have obviously done all the required thingies to embed Python in C+
+ as per the Python documentation)
I can achieve what I want by changing the Python function to a number
of functions, each returning a single double variable and call them
one by one, using code like above, and then passing them into an array
in C++, but I would prefer a more elegant way of doing it, because my
return variable could have hundreds, if not thousands, of values..

Can somebody help me please.

Thanks

Pieter Steenekamp.
 
H

Hrvoje Niksic

pieter said:
I want to use a Python function that returns a double array in C++.

Return an array.array('d') object, and use the buffer protocol (for
example PyObject_AsReadBuffer()) to get the address of the underlying
array of native doubles.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top