“Python/docs/3.9/c-api/iter”的版本间差异

来自菜鸟教程
Python/docs/3.9/c-api/iter
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:迭代器协议 — Python 文档}}
 
<div id="iterator-protocol" class="section">
 
<div id="iterator-protocol" class="section">
  
 
<span id="iterator"></span>
 
<span id="iterator"></span>
= Iterator Protocol =
+
= 迭代器协议 =
  
There are two functions specifically for working with iterators.
+
有两个函数专门用于处理迭代器。
  
; int <code>PyIter_Check</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''o''<span class="sig-paren">)</span>
+
; <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyIter_Check</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span><span class="sig-paren">)</span><br />
: Return true if the object ''o'' supports the iterator protocol.
 
  
<dl>
+
: 如果对象 ''o'' 支持迭代器协议,则返回 true。 此功能总是成功。
<dt>[[../structures#c|PyObject]] *<code>PyIter_Next</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''o''<span class="sig-paren">)</span></dt>
 
<dd><p>''Return value: New reference.''</p>
 
<p>Return the next value from the iteration ''o''. The object must be an iterator
 
(it is up to the caller to check this). If there are no remaining values,
 
returns <code>NULL</code> with no exception set. If an error occurs while retrieving
 
the item, returns <code>NULL</code> and passes along the exception.</p></dd></dl>
 
  
To write a loop which iterates over an iterator, the C code should look
+
; [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyIter_Next</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span><span class="sig-paren">)</span><br />
something like this:
+
 
 +
: 返回迭代 ''o'' 的下一个值。 该对象必须是一个迭代器(由调用者来检查)。 如果没有剩余值,则返回 <code>NULL</code> 且未设置异常。 如果检索项目时发生错误,则返回 <code>NULL</code> 并传递异常。
 +
 
 +
要编写一个遍历迭代器的循环,C 代码应如下所示:
  
 
<div class="highlight-c notranslate">
 
<div class="highlight-c notranslate">
第24行: 第21行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>PyObject *iterator = PyObject_GetIter(obj);
+
<syntaxhighlight lang="c">PyObject *iterator = PyObject_GetIter(obj);
 
PyObject *item;
 
PyObject *item;
  
第45行: 第42行:
 
else {
 
else {
 
     /* continue doing useful work */
 
     /* continue doing useful work */
}</pre>
+
}</syntaxhighlight>
 +
 
 +
</div>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
 +
<div class="clearer">
 +
 +
  
 
</div>
 
</div>
  
[[Category:Python 3.9 中文文档]]
+
[[Category:Python 3.9 文档]]

2021年10月31日 (日) 04:50的最新版本

迭代器协议

有两个函数专门用于处理迭代器。

int PyIter_Check(PyObject *o)
如果对象 o 支持迭代器协议,则返回 true。 此功能总是成功。
PyObject *PyIter_Next(PyObject *o)
返回迭代 o 的下一个值。 该对象必须是一个迭代器(由调用者来检查)。 如果没有剩余值,则返回 NULL 且未设置异常。 如果检索项目时发生错误,则返回 NULL 并传递异常。

要编写一个遍历迭代器的循环,C 代码应如下所示:

PyObject *iterator = PyObject_GetIter(obj);
PyObject *item;

if (iterator == NULL) {
    /* propagate error */
}

while ((item = PyIter_Next(iterator))) {
    /* do something with item */
    ...
    /* release reference when done */
    Py_DECREF(item);
}

Py_DECREF(iterator);

if (PyErr_Occurred()) {
    /* propagate error */
}
else {
    /* continue doing useful work */
}