[C++-sig] With boost python tuple, how to loop over tuple items?

HOUSSEN Franck fghoussen at gmail.com
Thu Jan 30 15:25:44 EST 2020


This helps ! Got it to work ! Thanks guys. Code attached for people who
could need it.

Franck

Le jeu. 30 janv. 2020 à 15:46, stefan <stefan at seefeld.name> a écrit :

>
> 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,
> [image: Stefan]
>
> --
>
>       ...ich hab' noch einen Koffer in Berlin...
>
>
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> https://mail.python.org/mailman/listinfo/cplusplus-sig
>


-- 
Bonne journée,

Franck HOUSSEN
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20200130/b6b4c542/attachment.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/b6b4c542/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dummy.cpp
Type: text/x-c++src
Size: 353 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20200130/b6b4c542/attachment.cpp>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Makefile
Type: application/octet-stream
Size: 86 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20200130/b6b4c542/attachment.obj>


More information about the Cplusplus-sig mailing list