Embedding Python - Passing by Reference

A

andy

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
 
T

Terry Reedy

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

Nope. Python's name-object model is so far different from the named memory
block model of Fortran/C/etc that terms invented for the latter are
misleading when applied to Python. Argument objects (or the contents
thereof, or lists or dicts constructed therefrom) are bound to parameter
names. (See the archives for endless discussion of what to call this.)

| def foo(a):
| a = a + 1
|
| Will change the value of a in the calling function.

Nope. Try it with the interactive interpreter (or IDLE, etc, equivalent).
Only takes a minute to test whatever you meant by that.tjr


|
 
N

Neil Cerutti

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?

You've got the passing convention basically correct, but the
semantcs of 'a + 1' wrong.

'a + 1' evaluates to a new integer object equal to a+1. Then the
= binds the local a to that new integer. The object that
the calling a refers to is never modified, and the name that is
bound to it is not rebound.

If you had modified the object that the local a was bound to, it
would have the effect you are after. As it happens, some objects
are immutable and thus cannot be modified.
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):

You cannot do it. You'll have to insist on a boxed value of some
kind, like one stored in a list or an object.

Python equivalent:
.... x[0] = 'foo'
....
['foo']
 
A

andy

Thanks for the replies - I see that I completely misunderstood
"passing by reference" when discussing Python. It looks like wrapping
the object up in a list will be the path I go down as it remains
closer to the C API I am wrapping.

Thanks again!

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top