Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / whatsnew / node4.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="whatsnew24.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="whatsnew24.html" title='What's New in Python 2.4' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="node5.html" />
<link rel="prev" href="node3.html" />
<link rel="parent" href="whatsnew24.html" />
<link rel="next" href="node5.html" />
<meta name='aesop' content='information' />
<title>3 PEP 289: Generator Expressions</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="2 PEP 237: Unifying"
href="node3.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="What's New in Python"
href="whatsnew24.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="4 PEP 292: Simpler"
href="node5.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">What's New in Python 2.4</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'><img src='../icons/blank.png'
border='0' height='32' alt='' width='32' /></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node3.html">2 PEP 237: Unifying</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="whatsnew24.html">What's New in Python</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node5.html">4 PEP 292: Simpler</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION000400000000000000000">
3 PEP 289: Generator Expressions</A>
</H1>
<P>
The iterator feature introduced in Python 2.2 and the
<tt class="module">itertools</tt> module make it easier to write programs that loop
through large data sets without having the entire data set in memory
at one time. List comprehensions don't fit into this picture very
well because they produce a Python list object containing all of the
items. This unavoidably pulls all of the objects into memory, which
can be a problem if your data set is very large. When trying to write
a functionally-styled program, it would be natural to write something
like:
<P>
<div class="verbatim"><pre>
links = [link for link in get_all_links() if not link.followed]
for link in links:
...
</pre></div>
<P>
instead of
<P>
<div class="verbatim"><pre>
for link in get_all_links():
if link.followed:
continue
...
</pre></div>
<P>
The first form is more concise and perhaps more readable, but if
you're dealing with a large number of link objects you'd have to write
the second form to avoid having all link objects in memory at the same
time.
<P>
Generator expressions work similarly to list comprehensions but don't
materialize the entire list; instead they create a generator that will
return elements one by one. The above example could be written as:
<P>
<div class="verbatim"><pre>
links = (link for link in get_all_links() if not link.followed)
for link in links:
...
</pre></div>
<P>
Generator expressions always have to be written inside parentheses, as
in the above example. The parentheses signalling a function call also
count, so if you want to create a iterator that will be immediately
passed to a function you could write:
<P>
<div class="verbatim"><pre>
print sum(obj.count for obj in list_all_objects())
</pre></div>
<P>
Generator expressions differ from list comprehensions in various small
ways. Most notably, the loop variable (<var>obj</var> in the above
example) is not accessible outside of the generator expression. List
comprehensions leave the variable assigned to its last value; future
versions of Python will change this, making list comprehensions match
generator expressions in this respect.
<P>
<div class="seealso">
<p class="heading">See Also:</p>
<dl compact="compact" class="seerfc">
<dt><a href="http://www.python.org/peps/pep-0289.html"
title="Generator Expressions"
>PEP 289, <em>Generator Expressions</em></a>
<dd>Proposed by Raymond Hettinger and
implemented by Jiwon Seo with early efforts steered by Hye-Shik Chang.
</dl>
</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="2 PEP 237: Unifying"
href="node3.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="What's New in Python"
href="whatsnew24.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="4 PEP 292: Simpler"
href="node5.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">What's New in Python 2.4</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'><img src='../icons/blank.png'
border='0' height='32' alt='' width='32' /></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node3.html">2 PEP 237: Unifying</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="whatsnew24.html">What's New in Python</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node5.html">4 PEP 292: Simpler</A>
</div>
</div>
<hr />
<span class="release-info">Release 1.01.</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>