Python/C

Robin Boerdijk robin.boerdijk at nl.origin-it.com
Sat Oct 30 03:30:04 EDT 1999


Mona Wong <mona at eel.ucsd.edu> wrote in message
news:9910291656.ZM20777 at eel.ucsd.edu...
> Hi:
>
> I'm starting a project where I'll be extending Python with C.  I have a
> very beginner's question ...
>
> If I have an array I created in the Python interpreter, how can I
> access that array in my imported C module?
>
> Mona

Are you talking about an array or a list ? For a list, you can say something
like:

/* untested !! */

static PyObject* f(PyObject* self, PyObject* args)
{
  PyObject* list;
  int i;

  if (!PyArg_ParseTuple(args, "O!", &PyList_Type, &list))
    return NULL;

  i = 0;
  while (i < PyList_Size(list))
  {
    PyObject* element = PyList_GetItem(list, i);
    /* do something with element */
    i++;
  }

  Py_INCREF(Py_None);
  return Py_None;
}

Robin






More information about the Python-list mailing list