Boost python,从另一个类的方法返回一个类

Boost python, returning a class from a method of another class

本文关键字:一个 返回 python 另一个 Boost 方法      更新时间:2023-10-16

我的问题是我有两个类似的类

class B{
public:
    double x,y;
}
class A{
public:
    B foo();
}

BOOST_PYTHON_MODULE(interestingLib){
    class_<A>("A")
        .def("foo", &A::foo)
    ;
    class_<B>("B")
        .def_readonly("x",&B::x)
        .def_readonly("y",&B::y)
    ;
}

在我的python脚本中,我调用函数foo并得到返回,然后我想看到成员变量x,就像这个一样

A = interestingLib.A
B = A.foo()
B.x

但是我得到错误AttributeError: 'NoneType' object has no attribute 'x'

有人能告诉我问题出在哪里吗?

在python中,您应该使用

A = interestingLib.A()

而不是

A = interestingLib.A

此代码A = interestingLib.A分配给类型为Boost.Python.classA对象。使用A = interestingLib.A()可以创建具有默认构造函数的A类型的对象。