【问题标题】:Exposing boost::gregorian::date to Python using Boost.Python使用 Boost.Python 向 Python 公开 boost::gregorian::date
【发布时间】:2013-06-12 14:28:25
【问题描述】:

我想使用 Boost.Python 使 Python 可以使用 boost::gregorian::date。但是,当 Boost 日期类中没有一个函数时,如何创建一个像样的 __str__ 函数?我想这样写:

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

【问题讨论】:

    标签: boost-python boost-date-time


    【解决方案1】:

    经过一番研究,我找到了答案。您也可以为 .def 提供静态函数。只需给它to_iso_extended_string,它就会将对象作为第一个参数。

    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)
        ;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-10-25
      • 1970-01-01
      • 2013-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-26
      相关资源
      最近更新 更多