作者:虛壞叔叔
博客:https://xuhss.com
早餐店不會開到晚上,想吃的人早就來了!
在test.py
中添加一個類,用於被C++調用訪問:
print('test.py')
class TypePy:
def __init__(self):
print("TypePy init")
def test(self):
print("TypePy test")
{
// 獲取類
PyObject* TypePy = PyObject_GetAttrString(m, "TypePy");
if (!TypePy) {
throw exception("TypePy noe find!");
}
// 實例化對象 相當於調用構造函數__init__ 傳遞構造函數的參數NULL
PyObject *obj = PyObject_CallObject(TypePy, NULL);
if (!obj) {
throw exception("obj not Create!");
}
// 調用類成員函數
PyObject_CallMethod(obj, "test", 0);
Py_XDECREF(obj);
Py_XDECREF(TypePy);
}
運行:
可以看到輸出了想要的字符串
修改test.py
中的類TypePy
中的函數test的參數和返回值:
class TypePy:
def __init__(self):
print("TypePy init")
def test(self, arg1, arg2):
print("TypePy test")
print("arg1 = ", arg1)
print("arg2 = ", arg2)
return 1003
C++添加對單參數的Python類的調用
// 調用類成員函數 i(int) s(string)
PyObject *re = PyObject_CallMethod(obj, "test", "is", 2001, "c Para2");
cout << "PyObject_CallMethod return" << PyLong_AsLong(re) << endl;
Py_XDECREF(re);
運行:
添加test.py
中的類成員變量
class TypePy:
id = 99
def __init__(self):
print("TypePy init")
def test(self, arg1, arg2):
print("TypePy test")
print("arg1 = ", arg1)
print("arg2 = ", arg2)
return 1003
C++添加對Python類成員變量的訪問
// 訪問成員變量
PyObject* var = PyObject_GetAttrString(obj, "id");
cout << "TypePy.id=" << PyLong_AsLong(var) << endl;
Py_XDECREF(var);
運行:
#include <iostream>
#include <Python.h>
#include <exception>
using namespace std;
int main(int argc, char*argv[])
{
cout << "C++ call Python" << endl;
// 設置Python的Home路徑
Py_SetPythonHome(L"./");
// Python初始化解釋器
Py_Initialize();
PyObject *m = NULL; // 主模塊
try
{
int re = 0;
// 執行Python腳本
re = PyRun_SimpleString("print('Hello world!')");
re = PyRun_SimpleString("print(\"__name__ = \", __name__)");
// 執行Python文件
char* filename = "test.py";
FILE * fp = fopen(filename, "r");
if (!fp)
{
throw exception("open file failed");
}
PyRun_AnyFile(fp, filename);
if (re != 0)
{
PyErr_Print();
throw exception("PyRun_AnyFile failed");
}
// 獲取主模塊
PyObject *key = PyUnicode_FromString("__main__");
m = PyImport_GetModule(key); // 不清理參數,需要手動清理
Py_XDECREF(key);
// 2-1 調用python的變量 python做配置文件
//con = {
// "width":1920,
// "heigth" : 1080,
// "title" : "C++ call Python"
//}
{
// 根據模塊和名稱獲取對象(對象可以是變量、函數和類)
PyObject* conf = PyObject_GetAttrString(m, "conf");
if (!conf) {
throw exception("conf noe find!");
}
PyObject *key = PyUnicode_FromString("width");
int width = PyLong_AsLong(PyDict_GetItem(conf, key));
Py_XDECREF(key);
key = PyUnicode_FromString("height");
int height = PyLong_AsLong(PyDict_GetItem(conf, key));
Py_XDECREF(key);
key = PyUnicode_FromString("title");
wchar_t title[1024] = {
0 };
int size = PyUnicode_AsWideChar(PyDict_GetItem(conf, key), title, 1023);
Py_XDECREF(key);
printf("width=%d height=%d \n", width, height);
wprintf(L"title=%s\n", title);
Py_XDECREF(conf);
}
{
// 獲取類
PyObject* TypePy = PyObject_GetAttrString(m, "TypePy");
if (!TypePy) {
throw exception("TypePy noe find!");
}
// 實例化對象 相當於調用構造函數__init__ 傳遞構造函數的參數NULL
PyObject *obj = PyObject_CallObject(TypePy, NULL);
if (!obj) {
throw exception("obj not Create!");
}
// 調用類成員函數 i(int) s(string)
PyObject *re = PyObject_CallMethod(obj, "test", "is", 2001, "c Para2");
cout << "PyObject_CallMethod return" << PyLong_AsLong(re) << endl;
Py_XDECREF(re);
// 訪問成員變量
PyObject* var = PyObject_GetAttrString(obj, "id");
cout << "TypePy.id=" << PyLong_AsLong(var) << endl;
Py_XDECREF(var);
Py_XDECREF(obj);
Py_XDECREF(TypePy);
}
Py_XDECREF(m);
// 清理python
Py_Finalize();
}
catch (const std::exception&ex)
{
cout << ex.what() << endl;// 清理python
Py_XDECREF(m);
Py_Finalize();
}
getchar();
return 0;
}
點贊
收藏
轉發
一波哦,博主也支持為鐵粉絲制作專屬動態壁紙哦~關注下面卡片即刻獲取更多編程知識,包括各種語言學習資料,上千套PPT模板和各種游戲源碼素材等等資料。更多內容可自行查看哦!