Reputation: 725
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
Upvotes: 2
Views: 2149
Reputation: 2976
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.
class Value {
public:
Value() { }
Value(const char *val) { }
void append(const Value &val) { }
void append(const char *val) { }
};
There may be another neater solution but this seems to work for me.
Upvotes: 1