Embedding Python - Passing by Reference

andy at britishideas.com andy at britishideas.com
Thu Nov 29 17:39:52 EST 2007


I understand the parameters to Python functions are passed by
reference:

def foo(a):
  a = a + 1

Will change the value of a in the calling function. How do I implement
the equivalent in C when extending Python?

I know how to write a function that can be called from Python and I
know how to use PyArg_ParseTuple to get the value of a. But how do I
update the value of a in C? I tried (greatly simplified):

PyObject *a;
int value;
PyArg_ParseTuple(args, "O", &a);
value = PyLong_AsLong(a);
value++;
a = PyLong_FromLong(value);

but this doesn't work. Any suggestions?

Note that I am trying to wrap an existing C based API as closely as
possible, so I can't return the value using return (this example is
greatly simplified and I'm already using the return value for other
things).

Thanks! Andy



More information about the Python-list mailing list