1

I have a python module that I imported to C++ like this :

.cpp file:

boost::python::object mod;
void init_mod(boost::python::object o){
    mod = o;
}
// some code
BOOST_PYTHON_MODULE(MyModule){
    def("init_mod", &init_mod);
    // for the other code
}

from python, I import the module and assigns it:

from ..utilities import foo as foo_module
init_mod(foo_module)

In foo, I have a class named Foo, and a wrapper function that can return a Foo object

class Foo:
    def __init__(self, i, j, k):
        self._i = i
        self._j = j
        self._k = k
        self._done = False
def wrapper_foo(i, j, k):
    return Foo(i,j,k)

And in the same C++ file, I tried to create a Foo object but it returns None:

Bar* create_foo(){
    // this returns None
    boost::python::object foo = mod.attr("Foo")(1,2,3);
    foo.attr("_done") = true; // NoneType has no attribute _done error
    // this also returns None
    boost::python::object foo2 = mod.attr("wrapper_foo")(1,2,3);
    foo2.attr("_done") = true; // NoneType has no attribute _done error
    // some other code
    return bar_object;

How can I fix or work around this?

Thanks

qwerty_99
  • 640
  • 5
  • 20

0 Answers0