3

I have a C++ code called from Python. I have list of tuples passed from python:

boost::python::list<boost::python::tuple>

How do I access the elements of boost::python::tuple?

Any example would be handy. From the documentation, I can't find the accessor methods.

1
  • there must be a mapping function, and the methods are not fully documented. for a quick workaround try to build a custom wrapper (like this one for example) Commented Mar 9, 2021 at 8:19

1 Answer 1

0

I was able to use the boost::python::extract<int>( ) method.

For example for a python tuple containing an integer and a string:

Python side:

t = (123, 'some string')

C++ side:

void work_with_tuple(boost::python::tuple t)
{
   if (boost::python::len(t) != 2) throw std::invalid_argument("bad");

   int extracted_int = boost::python::extract<int>(t[0]);
   std::string extracted_string = boost::python::extract<std::string>(t[1]);
}   

                                             
Sign up to request clarification or add additional context in comments.

1 Comment

@Ramy You may wish to write a separate answer containing the elaboration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.