C++ 如何覆盖Boost::Python自动创建的docstring数据?
我目前正在为Python开发一个基于C++的模块。我发现Boost::Python对于我想要实现的目标来说运行得非常好。但是,我现在在Boost::Python生成的docstring中遇到了一些问题。给定以下Boost::Python定义:C++ 如何覆盖Boost::Python自动创建的docstring数据?,c++,python,boost,documentation,boost-python,C++,Python,Boost,Documentation,Boost Python,我目前正在为Python开发一个基于C++的模块。我发现Boost::Python对于我想要实现的目标来说运行得非常好。但是,我现在在Boost::Python生成的docstring中遇到了一些问题。给定以下Boost::Python定义: BOOST_PYTHON_MODULE(gcsmt) { class_<gcsmt::Units>("Units", "Sets the units used as input.", no_init) .def("PrintSuppor
BOOST_PYTHON_MODULE(gcsmt)
{
class_<gcsmt::Units>("Units", "Sets the units used as input.", no_init)
.def("PrintSupported", &gcsmt::Units::printSupported, "Print out all supported units.")
.def("SetDefault", &gcsmt::Units::setDefaultUnit, "Sets the default unit to be used for inputs/outputs.")
.staticmethod("PrintSupported")
.staticmethod("SetDefault")
.def(self_ns::str(self_ns::self))
;
}
- 使用boost::python::docstring_options类定义自动创建的docstring选项
- 所有def函数都将docstring作为最后一个参数
- 所有类定义都将类docstring作为最后一个参数
使用boost::python;
BOOST_PYTHON_模块(foo)
{
//这将启用用户定义的docstring和python签名,
//同时禁用C++签名
docstring_选项本地_docstring_选项(真、真、假);
类(“Bar”,init(),“Bar类”/*此处的类docstring*/)
.def(“foobar”、&Bar::foobar,“foobar函数”/*函数docstring here*/);
}
谢谢aleksey。docstring_选项让我大部分时间都在那里。python中的帮助模块似乎为类添加了大量额外信息,但这似乎不是一个增强问题。docstring\u options
是一个覆盖ctor中的选项并在dtor中恢复它们的类,有效地更改了当前范围中的选项。因此,仅创建一个临时的docstring\u options
,是不够的,您必须创建一个局部变量以使其保持活动状态,例如docstring\u options doc\u opts(true、true、false)代码>。在2017年仍然是一个有用的答案。请注意,类
的docstring与上面的稍有不同。它是类(“PyName”,“class docstring”,init(),“init docstring”)
。请看,我之前的评论表明这个答案很有用,但我当时的意思是对class.
的参数排序。关于关闭C++签名,我只是尝试了这个答案,没有任何效果。但是,通过检查,我能够看到<代码> DOCSSTRIN选项DOCOPT;docopt.enable_all();docopt.disable_cpp_signatures()代码>是我所需要的,并已确认有效
>>> help(gcsmt.Units)
Help on class Units in module gcsmt:
class Units(Boost.Python.instance)
| Sets the units used as input.
|
| Method resolution order:
| Units
| Boost.Python.instance
| __builtin__.object
|
| Methods defined here:
|
| __reduce__ = <unnamed Boost.Python function>(...)
|
| __str__(...)
| __str__( (Units)arg1) -> object :
|
| C++ signature :
| _object* __str__(gcsmt::Units {lvalue})
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| PrintSupported(...)
| PrintSupported() -> None :
| Print out all supported units.
|
| C++ signature :
| void PrintSupported()
|
| SetDefault(...)
| SetDefault( (UnitType)arg1, (str)arg2) -> None :
| Sets the default unit to be used for inputs/outputs.
|
| C++ signature :
| void SetDefault(gcsmt::unitType,std::string)
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __init__ = <built-in function __init__>
| Raises an exception
| This class cannot be instantiated from Python
|
| ----------------------------------------------------------------------
| Data descriptors inherited from Boost.Python.instance:
|
| __dict__
|
| __weakref__
|
| ----------------------------------------------------------------------
| Data and other attributes inherited from Boost.Python.instance:
|
| __new__ = <built-in method __new__ of Boost.Python.class object>
| T.__new__(S, ...) -> a new object with type S, a subtype of T
>>> help(gcsmt.Units)
Help on class Units in module gcsmt:
class Units
| Sets the units used as input.
|
| PrintSupported() -> None :
| Print out all supported units.
|
| SetDefault( (UnitType)arg1, (str)arg2) -> None :
| Sets the default unit to be used for inputs/outputs.
using boost::python;
BOOST_PYTHON_MODULE(foo)
{
// This will enable user-defined docstrings and python signatures,
// while disabling the C++ signatures
docstring_options local_docstring_options(true, true, false);
class_<Bar>("Bar", init<>(), "Bar class" /* class docstring here */ )
.def("foobar", &Bar::foobar, "foobar function" /* function docstring here */);
}