疊代器(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
IndexErrorfor 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.
Range 物件¶
- PyTypeObjectPyRange_Type¶
- 為穩定 ABI 的一部分.
range物件的型別物件。
內建疊代器型別¶
These are built-in iteration types that are included in Python's C API, butprovide no additional functions. They are here for completeness.
C 型別 | Python 型別 |
|---|---|
| |
| |
| |
| |
|
其他疊代器物件¶
- PyTypeObjectPyByteArrayIter_Type¶
- 為穩定 ABI 的一部分.
- PyTypeObjectPyBytesIter_Type¶
- 為穩定 ABI 的一部分.
- PyTypeObjectPyListIter_Type¶
- 為穩定 ABI 的一部分.
- PyTypeObjectPyListRevIter_Type¶
- 為穩定 ABI 的一部分.
- PyTypeObjectPySetIter_Type¶
- 為穩定 ABI 的一部分.
- PyTypeObjectPyTupleIter_Type¶
- 為穩定 ABI 的一部分.
- PyTypeObjectPyRangeIter_Type¶
- 為穩定 ABI 的一部分.
- PyTypeObjectPyLongRangeIter_Type¶
- 為穩定 ABI 的一部分.
- PyTypeObjectPyDictIterKey_Type¶
- 為穩定 ABI 的一部分.
- PyTypeObjectPyDictRevIterKey_Type¶
- 為穩定 ABI 的一部分 自 3.8 版本開始.
- PyTypeObjectPyDictIterValue_Type¶
- 為穩定 ABI 的一部分.
- PyTypeObjectPyDictRevIterValue_Type¶
- 為穩定 ABI 的一部分 自 3.8 版本開始.
- PyTypeObjectPyDictIterItem_Type¶
- 為穩定 ABI 的一部分.
- PyTypeObjectPyDictRevIterItem_Type¶
- 為穩定 ABI 的一部分 自 3.8 版本開始.
- PyTypeObjectPyODictIter_Type¶
各種內建物件的疊代器型別物件。
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 iteratortype. For example, iterating over
rangewill use one of two iteratortypes depending on the size of the range. Other types may start using asimilar scheme in the future, without warning.