문제

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