For an object to pickable, it needs to implement a __getstate__
and a __setstate__
function.
What I usually do is the following (at the end of your interface file). Say that the object MyObject
is the object that you have wrapped with SWIG for which the wrapper isn't pickable.
#pragma once
class MyOBject {
public:
MyObject() {}
SetA(float a) {m_a = a;}
SetB(float b) {m_b = b;}
private:
float m_a;
float m_b;
};
Then in your interface file, do the following
%extend MyObject {
%pythoncode %{
def __getstate__(self):
args = (a, b)
return args
def __setstate__(self, state)
self.__init__() # construct object
(a,b) = state
self.SetA(a)
self.SetB(b)
%}
}
For an object to be pickable, you must be able to recover it from the serialized data (in some way). In the above example, the recovering is made by calling a pair of functions. In your case, you need to find a way to serialize the state of your object and re-create it from the serialized data.
This shows how you can make a wrapper such that the objects become pickable, but this requires you to change the wrapper for libnl
Another option is to wrap the python objects that are passed around in a python class for which you implement __getstate__
and __setstate__
. The idea is the same, you need to be able to serialize and recover the state of your object. I am not sure which of the two approaches is the simplest one.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…