由SWIG包裝的C++對象在C++和Python之間來回傳遞自定義Python數據時,是否有辦法使用它們?例如:在SWIG中攜帶自定義Python數據的C++對象
example.h文件
class MyClass
{
public:
int foo;
};
// Black box functions.
// Only thing guaranteed is that the last object handed to consume
// will be returned by eject.
void consume(MyClass *obj);
MyClass *eject();
example.i
%module Example
%{
#include "example.h"
%}
%include "example.h"
test.py
import Example
a = Example.MyClass()
a.bar = "Puppies"
Example.consume(a)
b = Example.eject()
## Should output "Puppies"
print b.bar
的電流輸出是一個n「AttributeError:bar」。
有沒有辦法獲得這種功能?如果SWIG的__ getattr _可以自定義MyClass的函數,那麼可能存儲未知屬性並從內部PyObject *加載(通過自動使SWIG子類MyClass或通過在MyClass中存在此類對象)?
謝謝!