Boost::Python Forward Declaration of boost::python::object throwing python TypeError
Question: I'm trying to refactor parts of my project, particularly the Python/C++
interface. The standard boost::python python initialization was working
before:

    
    
    boost::python::object main_module = boost::python::import("__main__");
    boost::python::object globals(main_module.attr("__dict__"));
    

//...

However, after factoring that into a class of its own, I'm getting

    
    
    TypeError: No to_python (by-value) converter found for C++ type: boost::python::api::proxy<boost::python::api::attribute_policies>
    

When an instantiating a PyInterface object, as below:

    
    
    namespace py = boost::python;
    class PyInterface
    {
    private:
        py::object
            main_module,
            global,
            tmp;
        //...
    public:
        PyInterface();
        //...
    };
    
    PyInterface::PyInterface()
    {
        std::cout << "Initializing..." << std::endl;
        Py_Initialize();
        std::cout << "Accessing main module..." << std::endl;
        main_module = py::import("__main__");
        std::cout << "Retrieve global namespace..." << std::endl;
        global(main_module.attr("__dict__"));
        //...
    }
    
    //in test.cpp
    int main()
    {
        PyInterface python;
        //...
    }
    
    Running gives the following output:
    Initializing...
    Accessing main module...
    Retrieving global namespace...
    
    TypeError: No to_python (by-value) converter found for C++ type: boost::python::api::proxy<boost::python::api::attribute_policies>
    

The only thing I can think is that it has something to do with declaring
"globals" before using it. In which case, is there another way that I can do
this?


Answer: Ah! Fixed it.

Changing the call to globals in the constructor from

    
    
    globals(main_method.attr("__dict__"));
    

to using the assignment operator instead:

    
    
    globals = main_method.attr("__dict__");
    

Looking back, that seems perfectly obvious, but at least I know I wasn't the
only one stumped judging by the lack of anyone derping me.

