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

2
  • Can you provide a minimal reproducible example? Commented Aug 12, 2020 at 15:35
  • I'll try to get one as soon as possible Commented Aug 12, 2020 at 20:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.