On 2020-01-29 4:01 p.m., HOUSSEN Franck wrote:


  With boost python tuple, how to loop over tuple items? In dummy.cpp
  attached, I'd like to get line 8 to work. Afterwards, I noticed line
  7 does not even compile. How to get the length of a tuple (line 8),
  and, why there could be some ambiguity (line 7). Any help / clue is
  appreciated.

Hi Frank,

there are a few minor issues with your code, let me address them one-by-one to allow you to compile and run your example:

* you don't need to link your extension module with the Python library. Symbols from that library are provided by the interpreter itself. (You only need to link to that when you embed the interpreter into your C++ app.)

* you should not call `Py_Initialize()` during your module's initialization. Again, that function only needs to be called when you embed (and initialize) the interpreter itself.

* use `boost::python::len(t)` to report the length of your tuple

* the call `t[0]` itself is fine, but please be aware that it returns a `boost::python::object` instance. You can convert to a (Python) string and then extract the C++ string from that using

    std::string s = bpl::extract<std::string>(bpl::str(t[0]));

and print that.

Hope this helps,

Stefan

--

      ...ich hab' noch einen Koffer in Berlin...
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
https://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to