Reputation: 744
I use C++ and swig to do some calculations. To simplify it, lets assume it looks like this:
struct TestIt{
TestIt(float x):x(x){};
inline float& getIt() {return x;};
float x;
};
Now I want to use the the getIt()
function and print the float value.
With
testee = matching.TestIt(42)
print(testee.getIt())
I get
<Swig Object of type 'float *' at 0x1cb1690>
which makes sense, because getIt returns a reference. How can I dereference it/get a python float out of it (without changing the c++ code)?
Upvotes: 0
Views: 2869
Reputation: 182000
Have a look here and here. You'd write something like this in your SWIG module:
%pointer_class(float, floatp)
That will let you do this in your Python code:
print(testee.getIt().value())
You will need to change your SWIG module, or write one if you are swigging the C++ header file directly. But you can probably get away with simply including your C++ header from there.
Upvotes: 1