gpt4 book ai didi

python - swig 忽略继承的函数

转载 作者:太空宇宙 更新时间:2023-11-03 21:18:53 26 4
gpt4 key购买 nike

我正在使用 swig 生成到 YaST 的 python 绑定(bind)。我通过 %extend 将函数添加到名为 YCPValue 的父类中。Swig 还将这些父类函数添加到子类中(例如 YCPCode)。我不希望子类中包含这些父类函数,因此我使用 %ignore 来忽略每个函数。除了 swig 仍然包含我忽略的功能。这是因为父类上的 %extend 吗?有谁知道是否有办法仍然从 child 身上排除这些功能?

%include <ycp/YCPValue.h>
%extend YCPValue {
bool isCode() {
return (*($self))->isCode();
}
YCPCode asCode() {
return (*($self))->asCode();
}
}

%include <ycp/YCPCode.h>
%ignore YCPCode::isCode();
%ignore YCPCode::asCode();

这里的想法是,我可能会返回一个 YCPValue 类型,然后必须使用 isCode() 函数来查看它是什么,并使用 asCode() 将类型作为 YCPCode 对象返回。但是如果我返回 YCPCode 对象,那么我不想拥有这些额外的函数(因为它使 pydocs 变得困惑等)。这里还涉及到更多的子类,这只是一个例子。

最佳答案

您可以执行以下操作

%extend SomeChild {
// Your C++ extenstion...
bool isCode() {
return (*($self))->isCode();
}
// Next your python extension
%pythoncode %{
if _newclass:
__swig_dir__ = __swig_getmethods__.copy()
__swig_dir__.update(__swig_setmethods__)
__swig_dir__ = list(__swig_dir__.keys())
__swig_dir__.remove('asCode')
def __dir__(self):
return SomeChild.__swig_dir__
%}
};

这不是最优雅的解决方案,但我之前已经使用这些重载来过滤掉不打算被调用的函数。

关于python - swig 忽略继承的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54467045/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com