[C++-sig] getting a list of strings from a static method

Jim Bosch talljimbo at gmail.com
Thu Aug 25 19:40:01 CEST 2011


On 08/24/2011 10:07 PM, Josh Stratton wrote:
> I'm very new to boost python and trying to figure out how to properly
> get a list of strings from a static method in my main namespace.  I'm
> not sure if I need to extract the list from the object or if it's
> already a list.
>
> boost::python::object list = exec("Foo.extensions()",
> _pyMainNamespace); // where Foo.extensions() is a static method
> returning a list of strings
>

Something like this:

namespace bp = boost::python;
bp::object pyList = bp::exec("Foo.extensions()", _pyMainNamespace);
std::vector<std::string> vec(bp::len(pyList));
for (std::size_t i = 0; i < vec.size(); ++i) {
    vec[i] = bp::extract<std::string>(pyList[i]);
}

> My end goal is to have a QList of QStrings, but if I can somehow get
> them to a boost list of std::strings I can take it from there.

I'll leave converting to Qt stuff up to you.  Note that you can also do 
extract<char const *>(), which may be more efficient if you have really 
large strings and don't want to copy them.

Good Luck!

Jim Bosch



More information about the Cplusplus-sig mailing list