gpt4 book ai didi

c++ - Boost-python 如何将 C++ 类实例传递给 Python 类

转载 作者:太空狗 更新时间:2023-10-29 18:00:31 32 4
gpt4 key购买 nike

我是 boost python 的新手。我必须首先在 cpp 代码中初始化一个 cpp 类实例,然后将这个 cpp 实例传递给 python 代码,使用 python 类实例来调用它(cpp 实例)。我尝试过 Python/C API 方式,但失败了,所以我想知道如何将 c++ 类实例传递给 python 类。

以下是我的代码,改自boost python demo。

在 main.cpp 中

#include <python2.6/Python.h>
#include <boost/python.hpp>
#include <iostream>

using namespace boost::python;
using namespace std;

class World
{
private:
string name;
public:
void set(string name)
{
this->name = name;
}
void greet()
{
cout << "hello, I am " << name << endl;
}
};

typedef boost::shared_ptr< World > world_ptr;

BOOST_PYTHON_MODULE(hello)
{
class_<World>("World")
.def("greet", &World::greet)
.def("set", &World::set)
;

register_ptr_to_python<world_ptr>();
};

int main()
{
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");

world_ptr worldObjectPtr (new World);
worldObjectPtr->set("C++!");

try
{
inithello();
PyObject* pModule =PyImport_ImportModule("python");
PyObject* pDict = PyModule_GetDict(pModule);
PyObject* pClassHelloPython = PyDict_GetItemString(pDict, "Person");
PyObject* pInstanceHelloPython = PyInstance_New(pClassHelloPython, NULL, NULL);

PyObject_CallMethod(pInstanceHelloPython, "sayHi", NULL);
worldObjectPtr->greet();
PyObject_CallMethod(pInstanceHelloPython, "greetReset", "O", worldObjectPtr);
worldObjectPtr->greet();
}
catch (error_already_set)
{
PyErr_Print();
}

Py_Finalize();

return 0;
}

在 python.py 中

class Person:
def sayHi(self):
print 'hello from python'

def greetReset(self, instance):
instance.set('Python')

在上面的代码中,我想将 worldObjectPtr 传递给 pInstanceHelloPython,因此,pInstanceHelloPython 可以将 worldObjectPtr->name 设置为 Python。但我只是不知道该怎么做。预先感谢您的耐心等待!!

最佳答案

通过 boost::python::ptr 将对象指针传递给 python。这将阻止 python 解释器进行复制:

#include <boost/python.hpp>
#include <string>
#include <iostream>

using namespace boost::python;
using namespace std;

class World
{
private:
string name;
public:
void set(string name) {
this->name = name;
}
void greet() {
cout << "hello, I am " << name << endl;
}
};

typedef boost::shared_ptr< World > world_ptr;

BOOST_PYTHON_MODULE(hello)
{
class_<World>("World")
.def("greet", &World::greet)
.def("set", &World::set)
;
};

int main(int argc, char **argv)
{
Py_Initialize();
try {
PyRun_SimpleString(
"class Person:\n"
" def sayHi(self):\n"
" print 'hello from python'\n"
" def greetReset(self, instance):\n"
" instance.set('Python')\n"
);

world_ptr worldObjectPtr (new World);
worldObjectPtr->set("C++!");

inithello();
object o_main
= object(handle<>(borrowed(PyImport_AddModule("__main__"))));
object o_person_type = o_main.attr("Person");
object o_person = o_person_type();
object o_func1 = o_person.attr("sayHi");
o_func1();
object o_func2 = o_person.attr("greetReset");
o_func2(boost::python::ptr(worldObjectPtr.get()));
worldObjectPtr->greet();
}
catch (error_already_set) {
PyErr_Print();
}

Py_Finalize();

return 0;
}

关于c++ - Boost-python 如何将 C++ 类实例传递给 Python 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5055443/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com