[C++-sig] With boost python tuple, how to loop over tuple items?
stefan
stefan at seefeld.name
Thu Jan 30 09:45:55 EST 2020
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...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20200130/68d104ec/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.png
Type: image/png
Size: 1478 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20200130/68d104ec/attachment-0001.png>
More information about the Cplusplus-sig
mailing list