By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

Embedding Python - Passing by Reference

P: n/a
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
Nov 29 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 2007-11-29, an**@britishideas.com <an**@britishideas.comwrote:
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:
>>def foo(x):
.... x[0] = 'foo'
....
>>a = [0]
foo(a)
a
['foo']

--
Neil Cerutti
Nov 30 '07 #2

P: n/a

<an**@britishideas.comwrote in message
news:6e**********************************@e23g2000 prf.googlegroups.com...
|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
|

Nov 30 '07 #3

P: n/a
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
Nov 30 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.