[C++-sig] Returning values to Python in C++ reference arguments

Jason Addison jraddison at gmail.com
Mon May 25 21:42:43 CEST 2015


On Mon, May 25, 2015 at 3:39 AM, Trigve Siver via Cplusplus-sig
<cplusplus-sig at python.org> wrote:
>
>
>>
>> How can results be returned in function arguments?
>>
>
>
> I don't think that you can return arguments by reference/pointers in python. You could theoretically pass a mutable sequence (list) and push the objects there.
>

You can return results via function arguments from C to Python (see code below).

Why is something similar impossible with Boost Python?

my_module2.c
>>>>
/*
clang -c -I/usr/include/python2.7 my_module2.c
clang -dynamiclib -o my_module2.so -lpython2.7 my_module2.o
 */

void foo_bar(int *a, double *b) {
    *a = 12;
    *b = 6.2832;
}
<<<<

byref2.py
>>>>
from ctypes import *
my_module2 = CDLL('my_module2.so')

x = c_int(0)
y = c_double(0)

my_module2.foo_bar(byref(x), byref(y))

assert(x.value == 12)
assert(y.value == 6.2832)
<<<<

> But the common approach is to return tuple.


More information about the Cplusplus-sig mailing list