SWIG C++ 到 Python:生成 Python 列表的子类
SWIG C++ to Python: generate a subclass of Python list
我正在使用 SWIG (v3) 将 C++ 代码包装到 Python 3. 在 C++ 中,我有一个继承自 std::vector
的 class MyClass
。我想把它包装在 Python 中,这样得到的 Python class MyClass
是标准 Python [=20 的子 class =] class。这可能吗?
这是一个例子:
example.h
#include <vector>
#include <iostream>
using namespace std;
// List class
class MyList : public vector<int>
{
public:
// Init
MyList() : vector<int>() {
this->insert(this->begin(),2);
this->insert(this->begin(),1);
}
// Toy insert function
void toy_insert()
{
this->insert(this->begin(),0);
}
};
example.cpp
#include "example.h"
example.i
%module example
%{
#include "example.h"
%}
%include "std_vector.i"
using namespace std;
%template(MyVector) std::vector<int>; // Nothing known about base class 'vector< int >' if we remove std:: here
%typemap(out) MyList* {
int size = (*).size();
$result = PyList_New(size);
for (int i=0; i<size; i++) {
PyList_SetItem($result, i, PyInt_FromLong((*)[i]));
}
};
// List class
class MyList : public std::vector<int> // Nothing known about base class 'vector< int >' if we remove std:: here
{
public:
MyList();
void toy_insert();
};
run_example.py
import example
my_list = example.MyList()
print(my_list)
打印命令returns <example.MyList; proxy of [1, 2] >
,但这不是我想要的。理想情况下,这应该只是 return [1,2]
,但那样我仍然可以调用:
my_list.toy_insert()
print(my_list)
哪个应该 return [0,1,2]
.
感谢您的帮助
如我的评论所述,您可以覆盖 SWIG 类型的显示。这是一个粗略的例子:
%module x
%{
#include "example.h"
%}
%include <windows.i>
%include <std_vector.i>
%template(MyVector) std::vector<int>;
%include "example.h"
%extend MyList {
const char*__repr__()
{
// I was lazy.
return "fancy representation";
}
}
输出:
>>> import x
>>> t=x.MyList()
>>> t
fancy representation
>>> t[0]
1
>>> t[1]
2
>>> t.toy_insert()
>>> t[0]
0
>>> t[1]
1
>>> t[2]
2
我正在使用 SWIG (v3) 将 C++ 代码包装到 Python 3. 在 C++ 中,我有一个继承自 std::vector
的 class MyClass
。我想把它包装在 Python 中,这样得到的 Python class MyClass
是标准 Python [=20 的子 class =] class。这可能吗?
这是一个例子:
example.h
#include <vector>
#include <iostream>
using namespace std;
// List class
class MyList : public vector<int>
{
public:
// Init
MyList() : vector<int>() {
this->insert(this->begin(),2);
this->insert(this->begin(),1);
}
// Toy insert function
void toy_insert()
{
this->insert(this->begin(),0);
}
};
example.cpp
#include "example.h"
example.i
%module example
%{
#include "example.h"
%}
%include "std_vector.i"
using namespace std;
%template(MyVector) std::vector<int>; // Nothing known about base class 'vector< int >' if we remove std:: here
%typemap(out) MyList* {
int size = (*).size();
$result = PyList_New(size);
for (int i=0; i<size; i++) {
PyList_SetItem($result, i, PyInt_FromLong((*)[i]));
}
};
// List class
class MyList : public std::vector<int> // Nothing known about base class 'vector< int >' if we remove std:: here
{
public:
MyList();
void toy_insert();
};
run_example.py
import example
my_list = example.MyList()
print(my_list)
打印命令returns <example.MyList; proxy of [1, 2] >
,但这不是我想要的。理想情况下,这应该只是 return [1,2]
,但那样我仍然可以调用:
my_list.toy_insert()
print(my_list)
哪个应该 return [0,1,2]
.
感谢您的帮助
如我的评论所述,您可以覆盖 SWIG 类型的显示。这是一个粗略的例子:
%module x
%{
#include "example.h"
%}
%include <windows.i>
%include <std_vector.i>
%template(MyVector) std::vector<int>;
%include "example.h"
%extend MyList {
const char*__repr__()
{
// I was lazy.
return "fancy representation";
}
}
输出:
>>> import x
>>> t=x.MyList()
>>> t
fancy representation
>>> t[0]
1
>>> t[1]
2
>>> t.toy_insert()
>>> t[0]
0
>>> t[1]
1
>>> t[2]
2