Frage

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__", ???)
    ;
}
War es hilfreich?

Lösung

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)
    ;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top