| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="whatsnew24.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="whatsnew24.html" title='What's New in Python 2.4' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='last' href='about.html' title='About this document...' /> |
| 10 | <link rel='help' href='about.html' title='About this document...' /> |
| 11 | <link rel="next" href="node5.html" /> |
| 12 | <link rel="prev" href="node3.html" /> |
| 13 | <link rel="parent" href="whatsnew24.html" /> |
| 14 | <link rel="next" href="node5.html" /> |
| 15 | <meta name='aesop' content='information' /> |
| 16 | <title>3 PEP 289: Generator Expressions</title> |
| 17 | </head> |
| 18 | <body> |
| 19 | <DIV CLASS="navigation"> |
| 20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 22 | <tr> |
| 23 | <td class='online-navigation'><a rel="prev" title="2 PEP 237: Unifying" |
| 24 | href="node3.html"><img src='../icons/previous.png' |
| 25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 26 | <td class='online-navigation'><a rel="parent" title="What's New in Python" |
| 27 | href="whatsnew24.html"><img src='../icons/up.png' |
| 28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 29 | <td class='online-navigation'><a rel="next" title="4 PEP 292: Simpler" |
| 30 | href="node5.html"><img src='../icons/next.png' |
| 31 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 32 | <td align="center" width="100%">What's New in Python 2.4</td> |
| 33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 34 | href="contents.html"><img src='../icons/contents.png' |
| 35 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 36 | <td class='online-navigation'><img src='../icons/blank.png' |
| 37 | border='0' height='32' alt='' width='32' /></td> |
| 38 | <td class='online-navigation'><img src='../icons/blank.png' |
| 39 | border='0' height='32' alt='' width='32' /></td> |
| 40 | </tr></table> |
| 41 | <div class='online-navigation'> |
| 42 | <b class="navlabel">Previous:</b> |
| 43 | <a class="sectref" rel="prev" href="node3.html">2 PEP 237: Unifying</A> |
| 44 | <b class="navlabel">Up:</b> |
| 45 | <a class="sectref" rel="parent" href="whatsnew24.html">What's New in Python</A> |
| 46 | <b class="navlabel">Next:</b> |
| 47 | <a class="sectref" rel="next" href="node5.html">4 PEP 292: Simpler</A> |
| 48 | </div> |
| 49 | <hr /></div> |
| 50 | </DIV> |
| 51 | <!--End of Navigation Panel--> |
| 52 | |
| 53 | <H1><A NAME="SECTION000400000000000000000"> |
| 54 | 3 PEP 289: Generator Expressions</A> |
| 55 | </H1> |
| 56 | |
| 57 | <P> |
| 58 | The iterator feature introduced in Python 2.2 and the |
| 59 | <tt class="module">itertools</tt> module make it easier to write programs that loop |
| 60 | through large data sets without having the entire data set in memory |
| 61 | at one time. List comprehensions don't fit into this picture very |
| 62 | well because they produce a Python list object containing all of the |
| 63 | items. This unavoidably pulls all of the objects into memory, which |
| 64 | can be a problem if your data set is very large. When trying to write |
| 65 | a functionally-styled program, it would be natural to write something |
| 66 | like: |
| 67 | |
| 68 | <P> |
| 69 | <div class="verbatim"><pre> |
| 70 | links = [link for link in get_all_links() if not link.followed] |
| 71 | for link in links: |
| 72 | ... |
| 73 | </pre></div> |
| 74 | |
| 75 | <P> |
| 76 | instead of |
| 77 | |
| 78 | <P> |
| 79 | <div class="verbatim"><pre> |
| 80 | for link in get_all_links(): |
| 81 | if link.followed: |
| 82 | continue |
| 83 | ... |
| 84 | </pre></div> |
| 85 | |
| 86 | <P> |
| 87 | The first form is more concise and perhaps more readable, but if |
| 88 | you're dealing with a large number of link objects you'd have to write |
| 89 | the second form to avoid having all link objects in memory at the same |
| 90 | time. |
| 91 | |
| 92 | <P> |
| 93 | Generator expressions work similarly to list comprehensions but don't |
| 94 | materialize the entire list; instead they create a generator that will |
| 95 | return elements one by one. The above example could be written as: |
| 96 | |
| 97 | <P> |
| 98 | <div class="verbatim"><pre> |
| 99 | links = (link for link in get_all_links() if not link.followed) |
| 100 | for link in links: |
| 101 | ... |
| 102 | </pre></div> |
| 103 | |
| 104 | <P> |
| 105 | Generator expressions always have to be written inside parentheses, as |
| 106 | in the above example. The parentheses signalling a function call also |
| 107 | count, so if you want to create a iterator that will be immediately |
| 108 | passed to a function you could write: |
| 109 | |
| 110 | <P> |
| 111 | <div class="verbatim"><pre> |
| 112 | print sum(obj.count for obj in list_all_objects()) |
| 113 | </pre></div> |
| 114 | |
| 115 | <P> |
| 116 | Generator expressions differ from list comprehensions in various small |
| 117 | ways. Most notably, the loop variable (<var>obj</var> in the above |
| 118 | example) is not accessible outside of the generator expression. List |
| 119 | comprehensions leave the variable assigned to its last value; future |
| 120 | versions of Python will change this, making list comprehensions match |
| 121 | generator expressions in this respect. |
| 122 | |
| 123 | <P> |
| 124 | <div class="seealso"> |
| 125 | <p class="heading">See Also:</p> |
| 126 | |
| 127 | <dl compact="compact" class="seerfc"> |
| 128 | <dt><a href="http://www.python.org/peps/pep-0289.html" |
| 129 | title="Generator Expressions" |
| 130 | >PEP 289, <em>Generator Expressions</em></a> |
| 131 | <dd>Proposed by Raymond Hettinger and |
| 132 | implemented by Jiwon Seo with early efforts steered by Hye-Shik Chang. |
| 133 | </dl> |
| 134 | </div> |
| 135 | |
| 136 | <P> |
| 137 | |
| 138 | <DIV CLASS="navigation"> |
| 139 | <div class='online-navigation'> |
| 140 | <p></p><hr /> |
| 141 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 142 | <tr> |
| 143 | <td class='online-navigation'><a rel="prev" title="2 PEP 237: Unifying" |
| 144 | href="node3.html"><img src='../icons/previous.png' |
| 145 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 146 | <td class='online-navigation'><a rel="parent" title="What's New in Python" |
| 147 | href="whatsnew24.html"><img src='../icons/up.png' |
| 148 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 149 | <td class='online-navigation'><a rel="next" title="4 PEP 292: Simpler" |
| 150 | href="node5.html"><img src='../icons/next.png' |
| 151 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 152 | <td align="center" width="100%">What's New in Python 2.4</td> |
| 153 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 154 | href="contents.html"><img src='../icons/contents.png' |
| 155 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 156 | <td class='online-navigation'><img src='../icons/blank.png' |
| 157 | border='0' height='32' alt='' width='32' /></td> |
| 158 | <td class='online-navigation'><img src='../icons/blank.png' |
| 159 | border='0' height='32' alt='' width='32' /></td> |
| 160 | </tr></table> |
| 161 | <div class='online-navigation'> |
| 162 | <b class="navlabel">Previous:</b> |
| 163 | <a class="sectref" rel="prev" href="node3.html">2 PEP 237: Unifying</A> |
| 164 | <b class="navlabel">Up:</b> |
| 165 | <a class="sectref" rel="parent" href="whatsnew24.html">What's New in Python</A> |
| 166 | <b class="navlabel">Next:</b> |
| 167 | <a class="sectref" rel="next" href="node5.html">4 PEP 292: Simpler</A> |
| 168 | </div> |
| 169 | </div> |
| 170 | <hr /> |
| 171 | <span class="release-info">Release 1.01.</span> |
| 172 | </DIV> |
| 173 | <!--End of Navigation Panel--> |
| 174 | <ADDRESS> |
| 175 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 176 | </ADDRESS> |
| 177 | </BODY> |
| 178 | </HTML> |