C++ 调用 Python 的 API 解读

官方网站  https://docs.python.org/2.6/   

 C++ 调用 Python 的 API 解读_第1张图片  

选择你安装的python版本;


1.

Py_BuildValue("s", "YaoYin") 等于 python中的 定义了一个字符串'YaoYin'

2.

PyObject *pDict = PyDict_New(); 
PyDict_SetItemString(pDict, "Name", Py_BuildValue("s", "YaoYin")); 

等于

python中的

pDict = []

pDict["Name"] = "YaoYin"


3. PyObject *pArgs = PyTuple_New(1); 
PyTuple_SetItem(pArgs, 0, pDict);//0---序号将字典类型变量添加到参数元组中

等于python中的

pArgs = (pDist,)  注意逗号不能省


4.

pReturn = PyEval_CallObject(pFunc, pArgs);//调用函数

等于python中的

apply(pFunc,pArgs)或者pFunc(*pArgs)  说明: * 的含义是 pArgs为元组

5.

int size = PyDict_Size(pReturn);

等于python中的

size = len(pReturn)

6.

PyObject *pNewAge = PyDict_GetItemString(pReturn, "Age");

等价

pReturn['Age']

7.

int newAge;

PyArg_Parse(pNewAge, "i", &newAge);

将python类型转为c类型



44. 

你可能感兴趣的:(C++ 调用 Python 的 API 解读)