0

I export a std::map<int, std::string> with pybind11 to Python env. But I have no idea how to use it in Python.

This is my c++ codes:

#include <map>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>

namespace py = pybind11;
using namespace py::literals;
using std::string;
using MyMap_t = std::map<int, string>;

PYBIND11_MAKE_OPAQUE( MyMap_t ); //no effect
PYBIND11_MODULE( ModuleCpp, m ) {
    py::bind_map<MyMap_t>( m, "MyMap_t" );
};

Here is my python code:

import ModuleCpp

if __name__ == "__main__":
    mm = ModuleCpp.MyMap_t
    print( mm )
    mm[1] = "Hello" # error occurs

The error message is here:

<class 'ModuleCpp.MyMap_t'>

Traceback (most recent call last):

File "./py-main.py", line 9, in

mm[1] = "Hello"
 TypeError: 'pybind11_type' object does not support item assignment
Leon
  • 1,489
  • 1
  • 12
  • 31

0 Answers0