[英]How to wrap with boost-python a virtual class with a method returning a reference
[英]Boost-python How to pass a c++ class instance to a python class
我是新手來提升python。 我必須首先在cpp代碼中初始化一個cpp類實例,然后將這個cpp實例傳遞給python代碼,使用python類實例來調用它(cpp實例)。 我已經嘗試過Python / C API方法但是失敗了,所以我想知道如何將c ++類實例傳遞給python類。
以下是我的代碼,改進了boost python演示。
在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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.