I have a very simple C++ class which owns a std::vector. I want to expose this class by SWIG to python :
Arr.hpp:
#include <vector>
class Arr
{
public:
inline Arr() : _v() { }
inline void add(double v) { _v.push_back(v); }
inline double get(unsigned int i) const { return _v[i]; }
inline const std::vector<double>& getVector() const { return _v; }
private:
std::vector<double> _v;
};
arr.i:
%module pyarr
%include <std_vector.i>
namespace std {
%template(VectorDouble) vector<double>;
};
%include Arr.hpp
%{
#include "Arr.hpp"
%}
Makefile:
swig -c++ -python -naturalvar -o arr_wrap.cpp arr.i
g++ -std=c++11 -fpic -shared arr_wrap.cpp -I/usr/include/python3.5m -o _pyarr.so
test_arr.py:
#!/usr/bin/python3
from pyarr import *
a = Arr()
a.add(1.2)
a.add(2.3)
print("v[0]=",a.get(0))
print("v[1]=",a.get(1))
print("v as vector=",a.getVector())
print("v=",a)
When I execute the test_arr.py script, I obtain:
python3 test_arr.py
v[0]= 1.2
v[1]= 2.3
v as vector= (1.2, 2.3)
v= <pyarr.Arr; proxy of <Swig Object of type 'Arr *' at 0x7f6f6fa4bf00> >
What I have to do to make my Arr class behaving like a python tuple ? So that the last line will be :
v= (1.2, 2.3)
[Edit]: My question is not only for display purpose, but also for plotting an histogram or initializing a numpy array, etc...
Note that, following the proposed answer here (How to use a Python list to assign a std::vector in C++ using SWIG?), I have tried with and without %naturalvar in swig command line.