Handling variable number of arguments.

Michael P. Reilly arcege at shore.net
Thu Jun 3 14:31:46 CEST 1999

bhosu at my-deja.com wrote:
: Hi All,
:    I am embedding Python in an application(C++). I would like to pass
: variable number of args to the python function. I could do that from
: python by using def foo(*args). How do I parse these arguments inside my
: application? In PyARg_ParseTuple, I have to know exactly how many args
: to read etc. I am using Python v 1.4. I was looking at
: PyArg_ParseTupleAndKeywords but that needs the args to be a dictionary.
: I am looking a similar thingy for lists. A typeical scenario of the
: usage is like,
: import foo
: foo.set_args('bar','=','1','2','3')

: where foo is the module containing the method implementation.

: Thanks,
: Bhosu.

It looks like you already know how to use PyArg_ParseTuple, so you are
halfway there.  You process the args as a tuple itself.  For example,

static int foo_argc = 0;
static PyObject *foo_argv[];

static PyObject *
foo_set_args(PyObject *self, PyObject *args)
  { register int count, length;
    foo_argc = length = PyTuple_Size(args);
    for (count = 0; count < length; count++) {
      item = PyTuple_GetItem(args, count);
      foo_argv[count] = item;
    foo_argv[length] = NULL;
    return Py_None;

Is this kind of what you are looking for?

More information about the Python-list mailing list