[C++-sig] Conversion between boost::tuple and a python tuple

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Thu Jan 27 00:48:00 CET 2005

--- Colin Irwin <colin_irwin at hotmail.com> wrote:
>         void foo( boost::tuple<int, int, int> int_tuple );
> ...
> All help would be much appreciated.  The BPL is perfect for what I would
> like to do, however, I feel I'm lacking a bit of understanding.

You have the right ideas. The problem is there is not "the one best" answer.

If you have only one function like foo, write a "thin wrapper," i.e. a
standalone function similar to:

foo_wrapper(ExampleClass& self, boost::python::tuple int_tuple)
  // assert len(int_tuple) == 3
  // loop over elements of the tuple
  // use boost::python::extract<int>, copy value to boost_int_tuple


  .def("foo", foo_wrapper)

Note that the standalone C++ function is wrapped as a Python member function.

If you have many functions with int_tuple arguments you want to think about
writing a custom converter. This is an advanced subject, but we can walk you
through. In any case, try the thin wrapper first. You can use it as a starting
point for a custom converter.


Do you Yahoo!? 
Yahoo! Mail - You care about security. So do we. 

More information about the Cplusplus-sig mailing list