Python C API return more than one value / object

Posted by Grisu on Programmers See other posts from Programmers or by Grisu
Published on 2013-11-05T07:55:57Z Indexed on 2013/11/05 10:11 UTC
Read the original article Hit count: 399

Filed under:
|

I got the following problem. I have written a C-Extension to Python to interface a self written software library. Unfortunately I need to return two values from the C function where the last one is optional. In Python the equivalent is

def func(x,y): 
    return x+y, x-y

test = func(13,4)    #only the first value is used

In my C extension I use

return Py_BuildValue("ii",x+y,x-y); 

which results in a tuple. If I now try to access the return value from Python via

test2 = cfunc(13,4)
print(test2)

I got a tuple instead of only the first return value. How is possible to build the same behavior as in Python from C Extension?

© Programmers or respective owner

Related posts about python

Related posts about c