473,473 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C API (embedded Python): How to get and set named variables

>From the C API (I'm using Python embedded), how can I get and set the
value of named variables? Right now, I'm using hacks like
PyRun_SimpleString("foobar = 12\n"), but I'd really like to access the
named objects directly.

Sep 10 '07 #1
2 2681
mr*****@gmail.com wrote:
>>From the C API (I'm using Python embedded), how can I get and set the
value of named variables? Right now, I'm using hacks like
PyRun_SimpleString("foobar = 12\n"), but I'd really like to access the
named objects directly.
You can use the following C functions to set/get named attributes of an
object:

PyObject_SetAttrString
PyObject_GetAttrString

If the attributes belong to the global scope of a module, then you can
use "PyImport_AddModule" to get a handle to the module object. For
example, if you wanted to get the value of an integer in the __main__
module named "foobar", you would do the following:

PyObject *m = PyImport_AddModule("__main__");
PyObject *v = PyObject_GetAttrString(m,"foobar");

int foobar = PyInt_AsLong(v);

Py_DECREF(v);

You will probably want to add some error checking in your code.

-Farshid
Sep 10 '07 #2
Thank you! Just what I needed.

I came across the PyObject_*AttrString() functions earlier when
searching through the docs, and I thought I was in the right area, but
I didn't know what object I was supposed to set the attribute for.
Now I see that it's a module object. It all makes sense now. Thanks!

Sep 10 '07 #3

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

Similar topics

14
by: David MacQuigg | last post by:
I am starting a new thread so we can avoid some of the non-productive argument following my earlier post "What is good about Prothon". At Mr. Hahn's request, I will avoid using the name "Prothon"...
0
by: Vio | last post by:
Hi, I've been trying to embed (statically) wxPy alongside an embedded py interpreter on a linux/gtk box. At one point, for some reason misc.o linking reported "multiple definitions of...
134
by: Joseph Garvin | last post by:
As someone who learned C first, when I came to Python everytime I read about a new feature it was like, "Whoa! I can do that?!" Slicing, dir(), getattr/setattr, the % operator, all of this was very...
2
by: fotis | last post by:
hello there! I am playing with embedded python these days. I wrote sth like this: ------------------------------ Code ------------------------------- #include <Python.h> #include <iostream>...
267
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at...
3
by: iwl | last post by:
Hello, I would like to add Variables to my embedded python which represents variables from my C++-Programm. I found C-Api-funcs for adding my C-Funcs to python but none to add variables. I...
0
by: Gabriel Genellina | last post by:
En Tue, 03 Jun 2008 16:58:12 -0300, Pau Freixes <pfreixes@milnou.net> escribió: So the above code corresponds to the standalone version - what about the embedded version? Are you sure it is...
4
by: Chris8Boyd | last post by:
I am embedding Python in a MSVC++ (2005) application. The application creates some environment and then launches a Python script that will call some functions exported from the MSVC++ application....
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
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.