SWIG:向生成的.py文件添加注释
使用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);
1 个回答
6