Вопрос

I'd like to make boost::gregorian::date available to Python using Boost.Python. But how do I create a decent __str__ function when one is not available on the Boost date class? I'd like to write it like this:

BOOST_PYTHON_MODULE(mymodule)
{
    class_<boost::gregorian::date>("Date")
        .add_property("year", &boost::gregorian::date::year)
        .add_property("month", &boost::gregorian::date::month)
        .def("__str__", ???)
    ;
}
Это было полезно?

Решение

After some research I found the answer. You can supply static functions to .def as well. Just give it to_iso_extended_string and it gets the object as first argument.

BOOST_PYTHON_MODULE(mymodule)
{
    class_<boost::gregorian::date>("Date")
        .add_property("year", &boost::gregorian::date::year)
        .add_property("month", &boost::gregorian::date::month)
        .def("__str__", &to_iso_extended_string)
    ;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top