I am attempting to create python bindings for some C++ code using swig. I seem have run into a problem trying to create python properties from some accessor functions I have for
I had the same problem and the advice to use %pythoncode worked for me. Here is what I did:
class Foo {
// ...
std::string get_name();
bool set_name(const std::string & name);
};
In the wrapper:
%include "foo.h"
%pythoncode %{
def RaiseExceptionOnFailure(mutator):
def mutator(self, v):
if not mutator(self, v):
raise ValueError("cannot set property")
return wrapper
Foo.name = property(Foo.get_name, RaiseExceptionOnFailure(Foo.set_name))
%}