I want to do something like this:
Py_Initialize();
PyRun_SimpleString("d = 0.97");
float a = ? // how to get value of d by its name?
Py_Finalize();
Is there a simple way to do this?
I want to do something like this:
Py_Initialize();
PyRun_SimpleString("d = 0.97");
float a = ? // how to get value of d by its name?
Py_Finalize();
Is there a simple way to do this?
PyRun_SimpleString
will not help AFAIK.
Use PyRun_String
instead of PyRun_SimpleString
which returns an object a pointer to a PyObject
. But this would not be very (simple) as mentioned by your question
As said tdelaney, "d
ends up in the __main__
module". So the best solution I find is
Py_Initialize();
PyRun_SimpleString("d = 0.97");
PyObject *mainModule = PyImport_AddModule("__main__");
PyObject *var = PyObject_GetAttrString(mainModule, "d");
float a = PyFloat_AsDouble(var);
Py_Finalize();
Not as simple as I expected, but acceptable and it works.