假设我在python模块中定义了一个类:
class A(object):
def __init__(self):
print 'init'
def method(self):
print 'method'
我想用boost :: python实例化该类的对象。我尝试了以下方式:
namespace py = boost::python;
// importing the module and extracting its namespace to
// the variable `ns`
...
py::object a = py::exec("A()", ns)
a.attr("method")()
打印init
然后崩溃。我在执行
py::object a = py::exec("A()", ns)
打印带有
的字符串表示std::cout << std::string(py::extract<std::string>(py::str(a))) << std::endl;
打印无。出了点问题。我该怎么做?
答案 0 :(得分:1)
我自己找到了答案:使用eval而不是exec。