【发布时间】:2014-04-21 22:03:40
【问题描述】:
我有一个数组类,Array1D,用 c++ 定义,它基本上包装了 STL 向量类。我扩展了这个类,以便可以显示数组向量的各个元素。这是我的 SWIG 接口文件中的相关代码:
namespace std{
%template(dblVector) vector<double>;
}
%extend Array1D{
double __getitem__(int index) {
return (*self)[index];
}
}
这允许我在 python 中访问数组的各个元素:
>>> a = Array1D(10) # creates a c++ vector of length 10 with zeros
>>> a[0]
>>> 0
例如,我希望能够调用 a[1:3],但是,当我尝试这个时,我得到了一个 TypeError:
TypeError: in method 'Array1D___getitem__', argument 2 of type 'int'
【问题讨论】: