ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](buffer.xhtml "缓冲协议") | - [上一页](mapping.xhtml "Mapping Protocol") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python/C API 参考手册](index.xhtml) » - [抽象对象层](abstract.xhtml) » - $('.inline-search').show(0); | # 迭代器协议 迭代器有两个函数。 int `PyIter_Check`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o*)返回 true , 如果对象 *o* 支持迭代器协议的话。 [PyObject](structures.xhtml#c.PyObject "PyObject")\* `PyIter_Next`([PyObject](structures.xhtml#c.PyObject "PyObject") *\*o*)*Return value: New reference.*返回迭代 *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 */ } ``` ### 导航 - [索引](../genindex.xhtml "总目录") - [模块](../py-modindex.xhtml "Python 模块索引") | - [下一页](buffer.xhtml "缓冲协议") | - [上一页](mapping.xhtml "Mapping Protocol") | - ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png) - [Python](https://www.python.org/) » - zh\_CN 3.7.3 [文档](../index.xhtml) » - [Python/C API 参考手册](index.xhtml) » - [抽象对象层](abstract.xhtml) » - $('.inline-search').show(0); | © [版权所有](../copyright.xhtml) 2001-2019, Python Software Foundation. Python 软件基金会是一个非盈利组织。 [请捐助。](https://www.python.org/psf/donations/) 最后更新于 5月 21, 2019. [发现了问题](../bugs.xhtml)? 使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 创建。