MOON
Server: Apache/2.2.31 (Unix) mod_ssl/2.2.31 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4
System: Linux csr818.wilogic.com 2.6.18-419.el5xen #1 SMP Fri Feb 24 22:50:37 UTC 2017 x86_64
User: digitals (531)
PHP: 5.4.45
Disabled: NONE
Upload Files
File: //usr/share/doc/python-docs-2.4.3/html/api/iterator.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="api.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="api.html" title='Python/C API Reference Manual' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="abstract-buffer.html" />
<link rel="prev" href="mapping.html" />
<link rel="parent" href="abstract.html" />
<link rel="next" href="abstract-buffer.html" />
<meta name='aesop' content='information' />
<title>6.5 Iterator Protocol </title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="6.4 Mapping Protocol"
  href="mapping.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="6. Abstract Objects Layer"
  href="abstract.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="6.6 Buffer Protocol"
  href="abstract-buffer.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python/C API Reference Manual</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='../icons/blank.png'
  border='0' height='32'  alt='' width='32' /></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="mapping.html">6.4 Mapping Protocol</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="abstract.html">6. Abstract Objects Layer</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="abstract-buffer.html">6.6 Buffer Protocol</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION008500000000000000000"></A><A NAME="iterator"></A>
<BR>
6.5 Iterator Protocol 
</H1>

<P>

<span class="versionnote">New in version 2.2.</span>

<P>
There are only a couple of functions specifically for working with
iterators.

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>int&nbsp;<b><tt id='l2h-345' xml:id='l2h-345' class="cfunction">PyIter_Check</tt></b>(</nobr></td><td>PyObject *<var>o</var>)</td></tr></table></dt>
<dd>
  Return true if the object <var>o</var> supports the iterator protocol.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject*&nbsp;<b><tt id='l2h-346' xml:id='l2h-346' class="cfunction">PyIter_Next</tt></b>(</nobr></td><td>PyObject *<var>o</var>)</td></tr></table></dt>
<dd>
<div class="refcount-info">
  <span class="label">Return value:</span>
  <span class="value">New reference.</span>
</div>
  Return the next value from the iteration <var>o</var>.  If the object is
  an iterator, this retrieves the next value from the iteration, and
  returns <tt class="constant">NULL</tt> with no exception set if there are no remaining
  items.  If the object is not an iterator, <tt class="exception">TypeError</tt> is
  raised, or if there is an error in retrieving the item, returns
  <tt class="constant">NULL</tt> and passes along the exception.
</dd></dl>

<P>
To write a loop which iterates over an iterator, the C code should
look something like this:

<P>
<div class="verbatim"><pre>
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 */
}
</pre></div>

<P>

<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="6.4 Mapping Protocol"
  href="mapping.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="6. Abstract Objects Layer"
  href="abstract.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="6.6 Buffer Protocol"
  href="abstract-buffer.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python/C API Reference Manual</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='../icons/blank.png'
  border='0' height='32'  alt='' width='32' /></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="mapping.html">6.4 Mapping Protocol</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="abstract.html">6. Abstract Objects Layer</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="abstract-buffer.html">6.6 Buffer Protocol</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.3, documentation updated on 29 March 2006.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>