疊代器(Iterator)物件¶
Python 提供了兩種通用的疊代器 (iterator) 物件,第一種是序列疊代器 (sequence iterator),適用於支援__getitem__()
方法的任意序列,第二種是與可呼叫 (callable) 物件和哨兵值 (sentinel value) 一起使用,會呼叫序列中的每個可呼叫物件,當回傳哨兵值時就結束疊代。
- PyTypeObjectPySeqIter_Type¶
- 為穩定 ABI 的一部分.
此型別物件用於由
PySeqIter_New()
所回傳的疊代器物件以及用於內建序列型別的內建函式iter()
的單引數形式。
- intPySeqIter_Check(PyObject*op)¶
Return true if the type ofop is
PySeqIter_Type
. This functionalways succeeds.
- PyObject*PySeqIter_New(PyObject*seq)¶
- 回傳值:新的參照。 為穩定 ABI 的一部分.
Return an iterator that works with a general sequence object,seq. Theiteration ends when the sequence raises
IndexError
for the subscriptingoperation.
- PyTypeObjectPyCallIter_Type¶
- 為穩定 ABI 的一部分.
Type object for iterator objects returned by
PyCallIter_New()
and thetwo-argument form of theiter()
built-in function.
- intPyCallIter_Check(PyObject*op)¶
Return true if the type ofop is
PyCallIter_Type
. Thisfunction always succeeds.
- PyObject*PyCallIter_New(PyObject*callable,PyObject*sentinel)¶
- 回傳值:新的參照。 為穩定 ABI 的一部分.
Return a new iterator. The first parameter,callable, can be any Pythoncallable object that can be called with no parameters; each call to it shouldreturn the next item in the iteration. Whencallable returns a value equal tosentinel, the iteration will be terminated.