473,387 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Embedding Python - Passing by Reference

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
3 1263
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

<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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alicia Haumann | last post by:
I accidentally sent this to webmaster@python.org, so this could be a duplicate if "webmaster" forwards it to this list. :{ Hi, there. Thanks for any help that can be offered. I've been...
8
by: Maurice LING | last post by:
Hi, anyone had any experiences in embedding python in python? I've tried to do this but it doesn't work. eval("from Tkinter import *") Thanks maurice
1
by: Craig Ringer | last post by:
Hi folks I'm a bit of a newbie here, though I've tried to appropriately research this issue before posting. I've found a lot of questions, a few answers that don't really answer quite what I'm...
1
by: jeremito | last post by:
I am trying to learn how to extend and/or embed Python. I have looked at the document "Extending and Embedding the Python Interpreter" and also "Python/C API Reference Manual. In the examples...
4
by: David Abrahams | last post by:
I'm seeing highly surprising (and different!) behaviors of PyImport_ImportModule on Linux and Windows when used in a program with python embedding. On Linux, when attempting to import a module...
6
by: mistabean | last post by:
Hello, first of all, I am a programming newbie, especially in python... Onwards to the problem, I have been having difficulty embedding a python module into my C/C++ program. (just a test...
1
by: Benke | last post by:
Hello, I'm quite new to Python and embedding python in c++. I'm trying to write a function that i can use to call a python function. It should take 3 arguments, the name of the python file, the...
2
by: bappai | last post by:
Hello, I am trying to actually call a GUI from my C++ code which would have buttons and therefore can call functions from C++ again, ie extend the C++ code also. I am faced with a peculiar...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.