Boost::Python: List

From Sidvind
Jump to: navigation, search

Boost::Python series

Site: boost.org (Documentation)

[edit]

Converting to and from a python lists is easy. If you are after a quick and dirty way see the page about pointer conversion.

This is faster but requires more work since you might need to provide a wrapper for your functions. If you just need the function to be used by python this is the better way. Same goes for returning lists.

Importing[edit]

Code: Import a list into C++

  1. void getList(boost::python::list* list){
  2.     int n = boost::python::extract<int>(list->attr("__len__")());
  3.    
  4.     for ( int i = 0; i < n; i++ ){
  5.         int val = (boost::python::extract<int>((*list)[i]));
  6.     }
  7. }

I think the code basicly explains itself. First we get the number of elements in the list by accessing __len__ from the python list. Secondly we just access one element at a time and retrieves the value.

Exporting[edit]