3

How do I handle a Python function that returns multiple lists

Python script:

display():
    a = [ 'a','b','abc']
    b = [ 1 , 2 , 3]
    return [a,b]

snippet of the c function calling the above python function:

pValue,pValue1 = PyObject_CallObject(pFunc, pArgs);

Is this the right way to call the function ? But then I cannot use the objects to extract the list values. What am I doing wrong ?

3
  • 3
    in python return (a,b) returns a tuple. a single object. in C you are trying to read the results as if it is 2 objects. try returning [a,b] instead. since lists are easier to handle than tuples i think. my c is very rusty. Commented Aug 13, 2012 at 15:44
  • 1
    sample is not valid Python code. Commented Aug 13, 2012 at 15:46
  • 1
    C dosent do multiple returns my friend. Commented Aug 13, 2012 at 15:55

3 Answers 3

3
PyObject *ret;
ret=PyObject_CallObject(pFunc,pArgs);
PyObject *ob1,*ob2;

PyArg_ParseTuple(ret,"oo",ob1,ob2); //o-> pyobject |i-> int|s-> char*

This is how you do it.

Sign up to request clarification or add additional context in comments.

5 Comments

I tried this but it doesn't work for me. This is what I get when nI run my program :Exception exceptions.SystemError: 'new style getargs format but argument is not a tuple' in 'garbage collection' ignored
Even I dont have clear idea about this. I've worked with this long time ago. Try encapsulating the return in a tuple so that this instruction works. Something like: return ((a,b)) #Now the return will be a tuple Else, look for the argument parsing in the python docs. There are more than two different ways to achieve function calling and argument parsing.
If you are interested in returning lists then PyListObject is to be considered.
I'm using that to extract values from the list in m program. I got it working for a single list but am stuck when the python function sends more than one list back.
PyArg_ParseTuple(ret, "O|O:ref", &ob1, &ob2) worked for me.
2

All Python functions/methods return one 'thing', but that 'thing' can be a 'container'. You keep asking how to return multiple lists; what you want to do is return a tuple, or a list, which contains the 'multiple lists' you want to return. Then, in the calling code, you access the elements of the tuple or list to get to the 'multiple lists' you want to return.

1 Comment

Thanks. I'll try returning a list which contains multiple list.
1

You don't receive two objects, you receive a new reference to PyTuple that is actually returned by your function.

4 Comments

Is tuple the only way to send mutliple objects back to the c program ?
Can I send back a tuple of lists ?
You can send any object back to c program (in your example it was tuple)
how can I send multiple list back ? and how should my c program interpret it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.