SWIG:为生成的.py文件添加注释

kyl*_*ewm 6 c++ python swig comments

使用SWIG为C++应用程序生成Python接口,有没有办法让它在生成的.py文件中注释函数?我实际上将整个.h文件放入.i文件中,但是一个小例子:

%module example

bool do_something_interesting(int number, string text);
Run Code Online (Sandbox Code Playgroud)

swig -python example.i 产生:

...
def do_something_interesting(*args):
  return _example.do_something_interesting(*args)
do_something_interesting = _example.do_something_interesting
Run Code Online (Sandbox Code Playgroud)

理想情况下,我希望它能够使用原始方法签名自动添加注释

#bool do_something_interesting(int number, string text)
def do_something_interesting(*args):
  return _example.do_something_interesting(*args)
do_something_interesting = _example.do_something_interesting
Run Code Online (Sandbox Code Playgroud)

但我完全可以在某处写自己的评论(特别是如果评论可能以某种方式在.h文件中).我认为%pythonprepend可能是一个可能的解决方案,但它在函数定义中而不是在它之前插入代码.

编辑:这是我在.h文件中提出的.不是最漂亮的东西,但它会做:

#ifdef SWIG
   %pythonprepend do_something_interesting(int, string) %{
    """Do some interesting thing

    number:Int -- a number
    text:String -- some text

    """
  %}
#endif
bool do_something_interesting(int number, string text);
Run Code Online (Sandbox Code Playgroud)

ben*_*gan 6

查看SWIG for Python中的docstring功能.可能会在.i文件中放入以下行

%feature("autodoc", "1")
Run Code Online (Sandbox Code Playgroud)

会做你想做的事情(更多选项请参见http://www.swig.org/Doc2.0/Python.html#Python_nn65)