python - 当我使用 operator[] 时列出导致错误的项目

标签 python c++ list boost

我使用 boost 1.57 和 boost python。这是我的代码示例:

list records = call_method<list>(...);
object attr = records.attr("__len__")();
int n = extract<long>(attr);
for (int i = 0; i < n; ++i)
{
    records[i];
}//here cause error

在这个列表中,我存储了一些包含来自 Python 的字符串的元组。好像records[i] 超出这个范围会调用Py_DECREF 导致错误,那么我应该怎么做才能从项目中获取数据呢?

最佳答案

好像有些operator[]会导致这些现象,所以我不得不避免它并使用以下解决方案:

#include <boost/python/stl_iterator.hpp>

        list records = call_method<list>(...);
        stl_input_iterator<tuple> end;
        for (stl_input_iterator<tuple> itr(records); itr != end;++itr)
        {
            auto record = *itr;
            std::string tick = call_method<std::string>(record.ptr(), "__getitem__", 0);
            ...
        }

关于python - 当我使用 operator[] 时列出导致错误的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38761141/

相关文章:

c++ - 当类包含指向另一个对象的指针时重载赋值运算符

Android 和 Crypto++ AES 128 位加密结果不匹配

c# - 我的客户列表已填充,但不会显示在服务器端 Blazor 的 Foreach 循环中

.net - 列表、数组或其他什么?

list - 列表中的 Scala 与 F# 范围从 1 到 100000000

python - 无法下载网页的完整源代码

python - while 循环中的小代码冗余(感觉不干净)

python - 如何编写一个函数来接受 SWIG 中的 Fraction 对象?

c++ - 在运行时重写相同的类方法 C++

python - 如何在GAE数据存储中显示图像?