| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="typesseq.html" /> |
| 13 | <link rel="prev" href="typesnumeric.html" /> |
| 14 | <link rel="parent" href="types.html" /> |
| 15 | <link rel="next" href="typesseq.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>2.3.5 Iterator Types </title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="2.3.4.1 Bit-string Operations on" |
| 25 | href="bitstring-ops.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="2.3 Built-in Types" |
| 28 | href="types.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="2.3.6 Sequence Types " |
| 31 | href="typesseq.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="bitstring-ops.html">2.3.4.1 Bit-string Operations on</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="types.html">2.3 Built-in Types</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="typesseq.html">2.3.6 Sequence Types </A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION004350000000000000000"></A><A NAME="typeiter"></A> |
| 56 | <BR> |
| 57 | 2.3.5 Iterator Types |
| 58 | </H2> |
| 59 | |
| 60 | <P> |
| 61 | |
| 62 | <span class="versionnote">New in version 2.2.</span> |
| 63 | |
| 64 | <a id='l2h-155' xml:id='l2h-155'></a> |
| 65 | |
| 66 | <P> |
| 67 | Python supports a concept of iteration over containers. This is |
| 68 | implemented using two distinct methods; these are used to allow |
| 69 | user-defined classes to support iteration. Sequences, described below |
| 70 | in more detail, always support the iteration methods. |
| 71 | |
| 72 | <P> |
| 73 | One method needs to be defined for container objects to provide |
| 74 | iteration support: |
| 75 | |
| 76 | <P> |
| 77 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 78 | <td><nobr><b><tt id='l2h-152' xml:id='l2h-152' class="method">__iter__</tt></b>(</nobr></td> |
| 79 | <td><var></var>)</td></tr></table></dt> |
| 80 | <dd> |
| 81 | Return an iterator object. The object is required to support the |
| 82 | iterator protocol described below. If a container supports |
| 83 | different types of iteration, additional methods can be provided to |
| 84 | specifically request iterators for those iteration types. (An |
| 85 | example of an object supporting multiple forms of iteration would be |
| 86 | a tree structure which supports both breadth-first and depth-first |
| 87 | traversal.) This method corresponds to the <tt class="member">tp_iter</tt> slot of |
| 88 | the type structure for Python objects in the Python/C API. |
| 89 | </dl> |
| 90 | |
| 91 | <P> |
| 92 | The iterator objects themselves are required to support the following |
| 93 | two methods, which together form the <i class="dfn">iterator protocol</i>: |
| 94 | |
| 95 | <P> |
| 96 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 97 | <td><nobr><b><tt id='l2h-153' xml:id='l2h-153' class="method">__iter__</tt></b>(</nobr></td> |
| 98 | <td><var></var>)</td></tr></table></dt> |
| 99 | <dd> |
| 100 | Return the iterator object itself. This is required to allow both |
| 101 | containers and iterators to be used with the <tt class="keyword">for</tt> and |
| 102 | <tt class="keyword">in</tt> statements. This method corresponds to the |
| 103 | <tt class="member">tp_iter</tt> slot of the type structure for Python objects in |
| 104 | the Python/C API. |
| 105 | </dl> |
| 106 | |
| 107 | <P> |
| 108 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 109 | <td><nobr><b><tt id='l2h-154' xml:id='l2h-154' class="method">next</tt></b>(</nobr></td> |
| 110 | <td><var></var>)</td></tr></table></dt> |
| 111 | <dd> |
| 112 | Return the next item from the container. If there are no further |
| 113 | items, raise the <tt class="exception">StopIteration</tt> exception. This method |
| 114 | corresponds to the <tt class="member">tp_iternext</tt> slot of the type structure |
| 115 | for Python objects in the Python/C API. |
| 116 | </dl> |
| 117 | |
| 118 | <P> |
| 119 | Python defines several iterator objects to support iteration over |
| 120 | general and specific sequence types, dictionaries, and other more |
| 121 | specialized forms. The specific types are not important beyond their |
| 122 | implementation of the iterator protocol. |
| 123 | |
| 124 | <P> |
| 125 | The intention of the protocol is that once an iterator's |
| 126 | <tt class="method">next()</tt> method raises <tt class="exception">StopIteration</tt>, it will |
| 127 | continue to do so on subsequent calls. Implementations that |
| 128 | do not obey this property are deemed broken. (This constraint |
| 129 | was added in Python 2.3; in Python 2.2, various iterators are |
| 130 | broken according to this rule.) |
| 131 | |
| 132 | <P> |
| 133 | Python's generators provide a convenient way to implement the |
| 134 | iterator protocol. If a container object's <tt class="method">__iter__()</tt> |
| 135 | method is implemented as a generator, it will automatically |
| 136 | return an iterator object (technically, a generator object) |
| 137 | supplying the <tt class="method">__iter__()</tt> and <tt class="method">next()</tt> methods. |
| 138 | |
| 139 | <P> |
| 140 | |
| 141 | <DIV CLASS="navigation"> |
| 142 | <div class='online-navigation'> |
| 143 | <p></p><hr /> |
| 144 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 145 | <tr> |
| 146 | <td class='online-navigation'><a rel="prev" title="2.3.4.1 Bit-string Operations on" |
| 147 | href="bitstring-ops.html"><img src='../icons/previous.png' |
| 148 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 149 | <td class='online-navigation'><a rel="parent" title="2.3 Built-in Types" |
| 150 | href="types.html"><img src='../icons/up.png' |
| 151 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 152 | <td class='online-navigation'><a rel="next" title="2.3.6 Sequence Types " |
| 153 | href="typesseq.html"><img src='../icons/next.png' |
| 154 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 155 | <td align="center" width="100%">Python Library Reference</td> |
| 156 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 157 | href="contents.html"><img src='../icons/contents.png' |
| 158 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 159 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 160 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 161 | <td class='online-navigation'><a rel="index" title="Index" |
| 162 | href="genindex.html"><img src='../icons/index.png' |
| 163 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 164 | </tr></table> |
| 165 | <div class='online-navigation'> |
| 166 | <b class="navlabel">Previous:</b> |
| 167 | <a class="sectref" rel="prev" href="bitstring-ops.html">2.3.4.1 Bit-string Operations on</A> |
| 168 | <b class="navlabel">Up:</b> |
| 169 | <a class="sectref" rel="parent" href="types.html">2.3 Built-in Types</A> |
| 170 | <b class="navlabel">Next:</b> |
| 171 | <a class="sectref" rel="next" href="typesseq.html">2.3.6 Sequence Types </A> |
| 172 | </div> |
| 173 | </div> |
| 174 | <hr /> |
| 175 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 176 | </DIV> |
| 177 | <!--End of Navigation Panel--> |
| 178 | <ADDRESS> |
| 179 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 180 | </ADDRESS> |
| 181 | </BODY> |
| 182 | </HTML> |