How do I use python object in C++
Aaron "Castironpi" Brady
castironpi at gmail.com
Wed Sep 24 00:06:23 EDT 2008
On Sep 23, 9:30 pm, lixinyi... at gmail.com wrote:
> If the PyObject is a PyList, and all list items are strings,
> say a=['aaa','bbb','ccc']
>
> How can I have a
> myArray[0] = "aaa"
> myArray[1] = "bbb"
> myArray[2] = "ccc"
> in C++?
>
> Do I have to
> use PyModule_GetDict() to get the dict first?
> what about the next?
>
>
>
> > 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.
>
>
This one writes '[aaa, bbb, ccc]' to the console in two different
ways.
#include <Python.h>
int main() {
PyObject *list, *listrepr;
Py_Initialize();
/* first */
list= PyList_New( 3 );
PyList_SetItem( list, 0, PyString_FromString( "aaa" ) );
PyList_SetItem( list, 1, PyString_FromString( "bbb" ) );
PyList_SetItem( list, 2, PyString_FromString( "ccc" ) );
listrepr= PyObject_Repr( list );
printf( "%s\n", PyString_AsString( listrepr ) );
Py_DECREF( listrepr );
Py_DECREF( list );
/* second */
list= Py_BuildValue( "[sss]", "aaa", "bbb", "ccc" );
listrepr= PyObject_Repr( list );
printf( "%s\n", PyString_AsString( listrepr ) );
Py_DECREF( listrepr );
Py_DECREF( list );
Py_Finalize();
return 0;
}
Did you want to execute some Python code, and examine variables it
creates?
More information about the Python-list
mailing list