How do I use python object in C++

Aaron "Castironpi" Brady castironpi at gmail.com
Wed Sep 24 03:47:08 CEST 2008


On Sep 23, 7:50 pm, lixinyi... at gmail.com wrote:
> for example I have the following code:
>
> #include <Python.h>
>
> void exec_pythoncode( int arg, char**argv )
> {
>     Py_Initialize();
>     Py_Main(argc,argv);
>     Py_Finalize();
>
> }
>
> What I would like to know is how can I get the variables I want
> after Py_Main(argc,argv)
>
> say I have a=[1,2,'Hello World',0.1234] in the python space,
> I would like to have it as a struct in C++.
>
> Any sample code?
> Thanks in advance!

What do you know about the contents of 'argc' and 'argv'?  If it's
impossible with Py_Main, can you use one of the other entry points?

If you've never manipulated PyObject* objects in C, they can be
hairy.  Even if you know 'a' is a sequence, its contents are still all
PyObject*s, which you can access via PyList_... and PySequence_
functions.



More information about the Python-list mailing list