【问题标题】:SWIG: Add comments to generated .py fileSWIG:向生成的 .py 文件添加注释
【发布时间】:2011-12-21 12:18:46
【问题描述】:

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

%module example

bool do_something_interesting(int number, string text);

swig -python example.i 生成:

...
def do_something_interesting(*args):
  return _example.do_something_interesting(*args)
do_something_interesting = _example.do_something_interesting

理想情况下,我希望它自动添加带有原始方法签名的注释

#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

但我完全可以在某处写我自己的评论(特别是如果评论可能以某种方式位于 .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);

【问题讨论】:

    标签: c++ python comments swig


    【解决方案1】:

    查看 SWIG for Python 中的文档字符串功能。 可能会将以下行放入您的 .i 文件中

    %feature("autodoc", "1")
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-11
      • 2013-04-11
      • 2022-06-23
      • 2016-10-13
      • 1970-01-01
      • 2023-01-12
      相关资源
      最近更新 更多