C++ 调用 Python 的 API 解读
2014-04-19 03:12
411 查看
官方网站 https://docs.python.org/2.6/
选择你安装的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.
选择你安装的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++调用PythonAPI线程状态和全局解释器锁
- C++调用PythonAPI线程状态和全局解释器锁(转)
- C++调用PythonAPI线程状态和全局解释器锁
- C++调用PythonAPI线程状态和全局解释器锁
- C++调用PythonAPI线程状态和全局解释器锁(转)
- SWIG之为C/C++的API生成Python调用接口基础
- paip.java c# .net php python调用c++ c dll so windows api 总结
- paip.java c# .net php python调用c++ c dll so windows api 总结
- python调用C和C++代码(BOOST)
- C++调用Python Py_Initialize失败
- c++调用python函数
- python使用ctypes调用C/C++
- linux下python 发送邮件,并通过C++调用
- vs15下c++调用python module
- Python调用C/C++动态链接库
- Python调用C++
- python调用C和C++代码
- C++调用Python脚本上传三维模型至Sketchfab网站
- python爬虫之百度API调用方法
- C++调用Python