【发布时间】:2012-02-12 09:17:11
【问题描述】:
我在网上搜索并没有成功。我将下面的示例代码包装到 Python(使用 SWIG):
class atomo {
public:
int i;
atomo(int a) {
i = a;
};
};
class funa {
public:
atomo *lista[3];
funa() {
lista[0] = new atomo(1);
lista[1] = new atomo(2);
lista[2] = new atomo(3);
};
};
但 Python 无法使用命令迭代或访问 lista
>>> test = myModule.funa()
>>> test.lista[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in __iter__
TypeError: 'SwigPyObject' object is not subscriptable
>>> for i in test.lista:
>>> print(i)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in __iter__
TypeError: 'SwigPyObject' object is not subscriptable
如何使lista 可迭代?有没有办法使用 Python 列表而不是 C++ 数组?
我的 Python 版本是 3.2,我正在使用 SWIG 2.0.4 和 g++ 4.6.1
谢谢
【问题讨论】:
-
只是猜测:
list(test.lista)
标签: c++ python arrays object swig