0

I am calling python from c++ using PyObject_CallObject

as the python return only a floating point number, i can get it by:

float output_of_python;
   PyObject *pValue,*pArgs;
  pValue = PyObject_CallObject(pFunc, pArgs);
 PyArg_Parse(pValue, "f", &output_of_python);

however, if python's returns 2 (return first,second), which format of PyArg_Parse(pValue... should i use to assign them to 2 c++ float variables?

p/s: this one does not work: PyArg_Parse(pValue, "f,f", &output_of_python1,&output_of_python2);

abcd
  • 11
  • 5
  • Not that I know the definite answer but I googled a bit upon my idea: If multiple values are returned, that's a tuple. ([How can I return two values from a function in Python?](https://stackoverflow.com/a/9752970/7478597)). This sounds as if the tuple can be handled like any iterable. – Scheff's Cat Jun 28 '22 at 17:01
  • I wonder that `PyArg_Parse()` works. I found it in the doc. So far, I always used [PyArg_ParseTuple()](https://docs.python.org/3/c-api/arg.html?highlight=pyarg_parsetuple#c.PyArg_ParseTuple). From the doc. I got the impression that these functions are dedicated for Python function argument tuples. However, ignoring this , and if `PyArg_Parse()` uses the same kind of format string like `PyArg_ParseTuple()`, it should've been `"ff"` (instead of `"f,f"`)... – Scheff's Cat Jun 28 '22 at 17:05
  • @Scheff'sCat thank, I just tried, the same effect as "f,f" (the variables are not changed after calling the function) – abcd Jun 28 '22 at 18:18
  • Almost all Python C API functions have ways of checking if they've raised an exception, which you look to be ignoring. These might help you identify your problem. – DavidW Jun 28 '22 at 19:15
  • For tuples, there is the [Tuple API](https://docs.python.org/3/c-api/tuple.html). I still believe that the Python tuple should support the [Iterator Protocol](https://docs.python.org/3.6/c-api/iter.html) but I failed to find a definite link for this. – Scheff's Cat Jun 29 '22 at 05:35

1 Answers1

0

i found it, post it here for anyone needs:

In c++, in order to get 2 variables from return of python, it is

float va1,va2;
PyArg_ParseTuple(pValue, "ff", &va1,&va2);
abcd
  • 11
  • 5