疊代器(Iterator)物件

Python 提供了兩種通用的疊代器 (iterator) 物件,第一種是序列疊代器 (sequence iterator),適用於支援 __getitem__() 方法的任意序列,第二種是與可呼叫 (callable) 物件和哨兵值 (sentinel value) 一起使用,會呼叫序列中的每個可呼叫物件,當回傳哨兵值時就結束疊代。

PyTypeObject PySeqIter_Type
穩定 ABI 的一部分.

此型別物件用於由 PySeqIter_New() 所回傳的疊代器物件以及用於內建序列型別的內建函式 iter() 的單引數形式。

int PySeqIter_Check(PyObject *op)

Return true if the type of op is PySeqIter_Type. This function always succeeds.

PyObject *PySeqIter_New(PyObject *seq)
回傳值:新的參照。穩定 ABI 的一部分.

Return an iterator that works with a general sequence object, seq. The iteration ends when the sequence raises IndexError for the subscripting operation.

PyTypeObject PyCallIter_Type
穩定 ABI 的一部分.

Type object for iterator objects returned by PyCallIter_New() and the two-argument form of the iter() built-in function.

int PyCallIter_Check(PyObject *op)

Return true if the type of op is PyCallIter_Type. This function always succeeds.

PyObject *PyCallIter_New(PyObject *callable, PyObject *sentinel)
回傳值:新的參照。穩定 ABI 的一部分.

Return a new iterator. The first parameter, callable, can be any Python callable object that can be called with no parameters; each call to it should return the next item in the iteration. When callable returns a value equal to sentinel, the iteration will be terminated.

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.