C API: passing by reference

Carsten Haese carsten at uniqsys.com
Sat Jun 23 14:47:38 EDT 2007


On Sat, 2007-06-23 at 18:25 +0000, stuart.tett at gmail.com wrote:
> I'm writing my own python extension module with the C API. In python
> all functions pass arguments by reference,

"Pass by reference", while correct from a certain standpoint, is to be
taken with a large grain of salt. It is correct in so far as the value
is not copied. It is incorrect in so far as, in general, you may not be
able to modify the object that's passed.

The reference you receive can only be used to call methods of the
referenced object, if it has any, or manipulate attributes of the
object, if it has any. It can not be used to replace the object with
another object.

>  but how can I make use of
> this in C? Right now, I am using:

You can't.

> PyArg_ParseTuple(args, "(ii)(ii)", &faceId1, &vertId1, &faceId2,
> &vertId2)
> 
> I want the to change the faceId's in my function. From what I've seen
> you can't do this safely with PyArg_ParseTuple.

Not with PyArg_ParseTuple, not with anything. Your function receives a
reference to an int object. Since int objects are immutable, you can't
replace the number that's in the int object.

> Do I have another option?

Return the value instead of trying to produce side-effects in the
caller's name space.

HTH,

-- 
Carsten Haese
http://informixdb.sourceforge.net





More information about the Python-list mailing list