//boxes.i
%module boxes
%inline %{
struct Box {
double length;
double breadth;
double height;
};
Box *newBox(double l,
double b,
double h) {
Box *box = new Box;
box->length = l;
box->breadth = b;
box->height = h;
return box;
}
void deleteBox(Box *box) {
delete box;
}
%}
Creating the module:
$ swig -c++ -python boxes.i
$ g++ -fPIC -c boxes_wrap.cxx -I/usr/include/python3.4
$ g++ -shared boxes_wrap.o -o _boxes.so
Using the module in Python3:
>>> import boxes
>>> x = boxes.newBox(3,2,5)
>>> type(x)
<class 'boxes.Box'>
>>> x.height
5.0
>>> x.height = 7
>>> x.height
7.0
>>> y = boxes.Box()
>>> y.length
0.0