Multi-Dimensional Arrays.

Adrian Eyre a.eyre at optichrome.com
Wed Nov 24 04:52:36 EST 1999


> Allow me to refine the question. If I make a list of lists to be my
> multi-dimensional array, how do I pass that into C?
> PyArg_ParseTuble(args, "?", &x)?

That should work.

> What would go in place of the "?"?

A big 'O'.

> Or would it be done a different way.  Then to pass it back to Python,
> I would return what?  Could I use Py_BuildVal("?", x) ? 

You can.

The PyArg_Parse and Py_BuildValue are really for converting to/from
C types. Since there is no obvious conversion for a Python list,
you must use the Python list API (function beginnig with PyList.)

Something like:

PyObject* myfunc(PyObject* self, PyObject* args)
{
	PyObject* mylist;
	int length, c;
	
	if (!PyArg_ParseTuple(args, "O", &mylist))
		return NULL;

	length = PyList_Size(mylist);

	for (c = 0; c < length; c++)
	{
		PyObject* item = PyList_GetItem(mylist, c);
		/* Do something with item */
	}

	/* Let's return the empty list */
	return PyList_New(0);
}

--------------------------------------------
Adrian Eyre <mailto:a.eyre at optichrome.com>
Optichrome Computer Solutions Ltd
Maybury Road, Woking, Surrey, GU21 5HX, UK
Tel: +44 1483 740 233  Fax: +44 1483 760 644
http://www.optichrome.com 
--------------------------------------------





More information about the Python-list mailing list