473,505 Members | 14,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Setting value of Python variable in a Python C extension

MD
Hi,
I have a function developed in C that is being used as a Python
extension. The function is being passed a variable from the Python
program. Is it possible to change the value of this variable from
within the C function?

Thanks,
-MD

Oct 1 '07 #1
3 1386
On Mon, 2007-10-01 at 09:08 -0700, MD wrote:
Hi,
I have a function developed in C that is being used as a Python
extension. The function is being passed a variable from the Python
program. Is it possible to change the value of this variable from
within the C function?
That question is ill-defined because Python doesn't have variables.
Python has objects and names. "Change the value of this variable" can
either mean that you want to mutate an object that's passed by the
caller, which you can do as long as the object is mutable, or you want
to rebind a name in the caller's namespace, which you can't do.

Why do you think your function needs to have side-effects instead of
simply returning a return value?

--
Carsten Haese
http://informixdb.sourceforge.net
Oct 1 '07 #2
MD
Hi Carsten,
Thanks for your reply. I am a newbie on Python so your help is
much appreciated. My program structure is basically like this .....
(rough representation)
test.py
var1 = ""
rc = func1 (var1)

test.c
func1(*v1) {
strcpy(v1, "Hello World");
}

So basically I want to modify value of "var1" in the function "func1"
defined in test.c. Is this possible?

Thanks,
-Manas

On Oct 1, 11:26 am, Carsten Haese <cars...@uniqsys.comwrote:
On Mon, 2007-10-01 at 09:08 -0700, MD wrote:
Hi,
I have a function developed in C that is being used as a Python
extension. The function is being passed a variable from the Python
program. Is it possible to change the value of this variable from
within the C function?

That question is ill-defined because Python doesn't have variables.
Python has objects and names. "Change the value of this variable" can
either mean that you want to mutate an object that's passed by the
caller, which you can do as long as the object is mutable, or you want
to rebind a name in the caller's namespace, which you can't do.

Why do you think your function needs to have side-effects instead of
simply returning a return value?

--
Carsten Haesehttp://informixdb.sourceforge.net

Oct 1 '07 #3
On Mon, 2007-10-01 at 09:42 -0700, MD wrote:
Hi Carsten,
Thanks for your reply. I am a newbie on Python so your help is
much appreciated. My program structure is basically like this .....
(rough representation)
test.py
var1 = ""
rc = func1 (var1)

test.c
func1(*v1) {
strcpy(v1, "Hello World");
}

So basically I want to modify value of "var1" in the function "func1"
defined in test.c. Is this possible?
No, that requires altering the caller's namespace, which is not
possible. Why do you think you need to do that? Is your problem that
func1 essentially returns two values and you don't know how to achieve
that? In that case, be advised that you can return a tuple to the
caller, along these lines:

test.py
rc, var1 = func1()

test.c
func1(void) {
/* ... */
return Py_BuildValue("(is)", rc, v1);
/* I'm assuming 'rc' is an integer, adjust the above line to fit
what rc actually is. */
}

Also, since you are a newbie, I'd like to suggest you work through the
Python tutorial and learn what you can do in pure Python first before
moving on to the more advanced topic of writing C extensions.

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net
Oct 1 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3838
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
3
3758
by: Matthias Baas | last post by:
Hi, are there any guidelines about what to do if a Windows extension for Python 2.4 requires the C++ runtime (msvcp71.dll)? If I want to distribute a binary installer of an extension that...
9
10945
by: runes | last post by:
Hi, I'm trying to set the title of the console window (CMD.EXE) in Windows. I want it set to the basename of the current directory and it should stay after the script has finished. Now, the...
3
3196
by: Sudheer Gupta | last post by:
Hi, I am having trouble using C struct in python. Hope anyone can help me out ... Say, I have my C struct as typedef struct call { struct call *next;
1
6441
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
0
312
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 408 open ( -9) / 3585 closed (+20) / 3993 total (+11) Bugs : 968 open ( +8) / 6505 closed ( +7) / 7473 total (+15) RFE : 267 open...
13
3982
by: c3950ig | last post by:
Hi, I am python newbie and the command prompt is having an issue with python. I installed python 2.4.4 onto my windows machine, opened a command prompt window, and typed python to start the...
0
2432
by: Akira Kitada | last post by:
Hi list, I was trying to build Python 2.6 on FreeBSD 4.11 and found it failed to build some of the modules. """ Failed to find the necessary bits to build these modules: _bsddb ...
0
1926
by: Akira Kitada | last post by:
Hi Marc-Andre, Thanks for the suggestion. I opened a ticket for this issue: http://bugs.python.org/issue4204 Now I understand the state of the multiprocessing module, but it's too bad to see...
0
7216
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
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7303
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
4699
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.