I need to add a new method to my swig template class, for example:
I am declaring a template class in myswig.i as follows:
%template(DoubleVector) vector<double>;
this will generate a class named “DoubleVector” in the generated .py file with some generated methods. lets suppose they are func1(), func2() and func3().
These are generated functions and i have no control over them.
Now, if I want to add a new method called “func4()” to this class(DoubleVector), how may I do it? Is it possible?
I know of a identifier called %pythoncode but I could not use it to define a new function in this template class.
Given an interface file like:
The easiest way to add more functionality to
DoubleVectoris to write it in C++, in the SWIG interface file using%extend:this has the advantage that it would work for any language you target with SWIG, not just Python.
You can also do it using
%pythoncodeand an unbound function:Example running this: