SWIG 和 C++构造函数
我想我一定在这里遗漏了一些东西,我希望有人可以提供帮助。 假设我有一个使用以下构造函数声明的 C++ 类:
class Value {
public:
Value();
Value(const char *val);
void append(const Value &val);
private:
...
};
创建一个简单的 SWIG 接口文件后,类似于:
%module(naturalvar=1) Value
%{
#include "value.h"
%}
%include "value.h"
为 Python 创建包装器代码 我可以这样做:
>>> import Value
>>> v1 = Value.Value()
>>> v2 = Value.Value("test")
>>> v1.append(v2)
>>> v1.append(Value.Value("test"))
但我不能这样做:
>>> v1.append("test")
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/jakob/dev/test/Value.py in append(self, *args)
298 def resize(self, *args): return _Value.Value_resize(self, *args)
299 def isValidIndex(self, *args): return _Value.Value_isValidIndex(self, *args)
--> 300 def append(self, *args): return _Value.Value_append(self, *args)
301 def get(self, *args): return _Value.Value_get(self, *args)
302 def removeMember(self, *args): return _Value.Value_removeMember(self, *args)
TypeError: in method 'Value_append', argument 2 of type 'Value const &'
显然它知道如何使用Value(const char *value) 构造函数,但如何让它理解传递给append 方法的字符串应该被馈送/转换为 Value 对象。
此致 雅各布·西蒙·加德
I think I must be missing something here, i hope someone can help.
Let's say I have a C++ class declared with the following constructors:
class Value {
public:
Value();
Value(const char *val);
void append(const Value &val);
private:
...
};
After creating a simple SWIG interface file, something like:
%module(naturalvar=1) Value
%{
#include "value.h"
%}
%include "value.h"
And creating wrapper code for Python I can do this:
>>> import Value
>>> v1 = Value.Value()
>>> v2 = Value.Value("test")
>>> v1.append(v2)
>>> v1.append(Value.Value("test"))
but I can't do this:
>>> v1.append("test")
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/jakob/dev/test/Value.py in append(self, *args)
298 def resize(self, *args): return _Value.Value_resize(self, *args)
299 def isValidIndex(self, *args): return _Value.Value_isValidIndex(self, *args)
--> 300 def append(self, *args): return _Value.Value_append(self, *args)
301 def get(self, *args): return _Value.Value_get(self, *args)
302 def removeMember(self, *args): return _Value.Value_removeMember(self, *args)
TypeError: in method 'Value_append', argument 2 of type 'Value const &'
Clearly it knows how to use the Value(const char *value) constructor, but how do I make it understand that the string passed to the append method should be fed/converted to a Value object.
Best regards
Jakob Simon-Gaarde
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Python 不会查看提供给函数的参数类型来确定要调用哪个函数(忽略方法分派)。所以我认为你不能让 python 做你想做的事。另一方面,当意识到超载时喝一口可以帮助你。如果你也重载append(),你就能得到你想要的。
可能还有另一个更简洁的解决方案,但这似乎对我有用。
Python doesn't look at the types of arguments given to a function to determine which function to call (ignoring method dispatch). So I don't think you can get python to do what you want. Swig on the other hand when is aware of overloading so can help you out. You can get what you want if you overload append() as well.
There may be another neater solution but this seems to work for me.