迭代器对象

Python 提供了两个通用迭代器对象。 第一个是序列迭代器,它可与支持 __getitem__() 方法的任意序列一起使用。 第二个迭代器使用一个可调用对象和一个哨兵值,为序列中的每个项目调用可调用对象,并在返回哨兵值时结束迭代。

PyTypeObject PySeqIter_Type
属于 稳定 ABI.

PySeqIter_New() 返回迭代器对象的类型对象和内置序列类型内置函数 iter() 的单参数形式。

int PySeqIter_Check(PyObject *op)

如果 op 的类型为 PySeqIter_Type 则返回真值。 此函数总是会成功执行。

PyObject *PySeqIter_New(PyObject *seq)
返回值:新的引用。 属于 稳定 ABI.

返回一个与常规序列对象一起使用的迭代器 seq。 当序列订阅操作引发 IndexError 时,迭代结束。

PyTypeObject PyCallIter_Type
属于 稳定 ABI.

由函数 PyCallIter_New()iter() 内置函数的双参数形式返回的迭代器对象类型对象。

int PyCallIter_Check(PyObject *op)

如果 op 的类型为 PyCallIter_Type 则返回真值。 此函数总是会成功执行。

PyObject *PyCallIter_New(PyObject *callable, PyObject *sentinel)
返回值:新的引用。 属于 稳定 ABI.

返回一个新的迭代器。 第一个参数 callable 可以是任何可以在没有参数的情况下调用的 Python 可调用对象;每次调用都应该返回迭代中的下一个项目。 当 callable 返回等于 sentinel 的值时,迭代将终止。

Range 对象

PyTypeObject PyRange_Type
属于 稳定 ABI.

range 对象的类型对象。

int PyRange_Check(PyObject *o)

如果对象 orange 对象的实例则返回真值。 此函数总是会成功执行。

内置迭代器类型

These are built-in iteration types that are included in Python's C API, but provide no additional functions. They are here for completeness.

C 类型

Python 类型

PyTypeObject PyEnum_Type
属于 稳定 ABI.

enumerate

PyTypeObject PyFilter_Type
属于 稳定 ABI.

filter

PyTypeObject PyMap_Type
属于 稳定 ABI.

map

PyTypeObject PyReversed_Type
属于 稳定 ABI.

reversed

PyTypeObject PyZip_Type
属于 稳定 ABI.

zip

Other Iterator Objects

PyTypeObject PyByteArrayIter_Type
属于 稳定 ABI.
PyTypeObject PyBytesIter_Type
属于 稳定 ABI.
PyTypeObject PyListIter_Type
属于 稳定 ABI.
PyTypeObject PyListRevIter_Type
属于 稳定 ABI.
PyTypeObject PySetIter_Type
属于 稳定 ABI.
PyTypeObject PyTupleIter_Type
属于 稳定 ABI.
PyTypeObject PyRangeIter_Type
属于 稳定 ABI.
PyTypeObject PyLongRangeIter_Type
属于 稳定 ABI.
PyTypeObject PyDictIterKey_Type
属于 稳定 ABI.
PyTypeObject PyDictRevIterKey_Type
属于 稳定 ABI 自 3.8 版起.
PyTypeObject PyDictIterValue_Type
属于 稳定 ABI.
PyTypeObject PyDictRevIterValue_Type
属于 稳定 ABI 自 3.8 版起.
PyTypeObject PyDictIterItem_Type
属于 稳定 ABI.
PyTypeObject PyDictRevIterItem_Type
属于 稳定 ABI 自 3.8 版起.

Type objects for iterators of various built-in objects.

Do not create instances of these directly; prefer calling PyObject_GetIter() instead.

Note that there is no guarantee that a given built-in type uses a given iterator type. For example, iterating over range will use one of two iterator types depending on the size of the range. Other types may start using a similar scheme in the future, without warning.