Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="ref.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="ref.html" title='Python Reference Manual' /> | |
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="dict.html" /> | |
13 | <link rel="prev" href="lists.html" /> | |
14 | <link rel="parent" href="atoms.html" /> | |
15 | <link rel="next" href="dict.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>5.2.5 Generator expressions</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="5.2.4 List displays" | |
25 | href="lists.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="5.2 Atoms" | |
28 | href="atoms.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="5.2.6 Dictionary displays" | |
31 | href="dict.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 Reference Manual</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'><img src='../icons/blank.png' | |
38 | border='0' height='32' alt='' width='32' /></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="lists.html">5.2.4 List displays</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="atoms.html">5.2 Atoms</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="dict.html">5.2.6 Dictionary displays</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H2><A NAME="SECTION007250000000000000000"></A><A NAME="genexpr"></A> | |
56 | <BR> | |
57 | 5.2.5 Generator expressions | |
58 | </H2> | |
59 | <a id='l2h-349' xml:id='l2h-349'></a> | |
60 | <P> | |
61 | A generator expression is a compact generator notation in parentheses: | |
62 | ||
63 | <P> | |
64 | <dl><dd class="grammar"> | |
65 | <div class="productions"> | |
66 | <table> | |
67 | <tr> | |
68 | <td><a id='tok-generator_expression' xml:id='tok-generator_expression'>generator_expression</a></td> | |
69 | <td>::=</td> | |
70 | <td>"(" <a class='grammartoken' href="lists.html#tok-test">test</a> <a class='grammartoken' href="genexpr.html#tok-genexpr_for">genexpr_for</a> ")"</td></tr> | |
71 | <tr> | |
72 | <td><a id='tok-genexpr_for' xml:id='tok-genexpr_for'>genexpr_for</a></td> | |
73 | <td>::=</td> | |
74 | <td>"for" <a class='grammartoken' href="exprlists.html#tok-expression_list">expression_list</a> "in" <a class='grammartoken' href="lists.html#tok-test">test</a> | |
75 | [<a class='grammartoken' href="genexpr.html#tok-genexpr_iter">genexpr_iter</a>]</td></tr> | |
76 | <tr> | |
77 | <td><a id='tok-genexpr_iter' xml:id='tok-genexpr_iter'>genexpr_iter</a></td> | |
78 | <td>::=</td> | |
79 | <td><a class='grammartoken' href="genexpr.html#tok-genexpr_for">genexpr_for</a> | <a class='grammartoken' href="genexpr.html#tok-genexpr_if">genexpr_if</a></td></tr> | |
80 | <tr> | |
81 | <td><a id='tok-genexpr_if' xml:id='tok-genexpr_if'>genexpr_if</a></td> | |
82 | <td>::=</td> | |
83 | <td>"if" <a class='grammartoken' href="lists.html#tok-test">test</a> [<a class='grammartoken' href="genexpr.html#tok-genexpr_iter">genexpr_iter</a>]</td></tr> | |
84 | </table> | |
85 | </div> | |
86 | <a class="grammar-footer" | |
87 | href="grammar.txt" type="text/plain" | |
88 | >Download entire grammar as text.</a> | |
89 | </dd></dl> | |
90 | ||
91 | <P> | |
92 | A generator expression yields a new generator object. | |
93 | <a id='l2h-350' xml:id='l2h-350'></a><a id='l2h-351' xml:id='l2h-351'></a>It consists of a single expression followed by at least one | |
94 | <tt class="keyword">for</tt> clause and zero or more <tt class="keyword">for</tt> or <tt class="keyword">if</tt> | |
95 | clauses. The iterating values of the new generator are those that | |
96 | would be produced by considering each of the <tt class="keyword">for</tt> or | |
97 | <tt class="keyword">if</tt> clauses a block, nesting from left to right, and | |
98 | evaluating the expression to yield a value that is reached the | |
99 | innermost block for each iteration. | |
100 | ||
101 | <P> | |
102 | Variables used in the generator expression are evaluated lazily | |
103 | when the <tt class="method">next()</tt> method is called for generator object | |
104 | (in the same fashion as normal generators). However, the leftmost | |
105 | <tt class="keyword">for</tt> clause is immediately evaluated so that error produced | |
106 | by it can be seen before any other possible error in the code that | |
107 | handles the generator expression. | |
108 | Subsequent <tt class="keyword">for</tt> clauses cannot be evaluated immediately since | |
109 | they may depend on the previous <tt class="keyword">for</tt> loop. | |
110 | For example: "<tt class="samp">(x*y for x in range(10) for y in bar(x))</tt>". | |
111 | ||
112 | <P> | |
113 | The parentheses can be omitted on calls with only one argument. | |
114 | See section <A href="calls.html#calls">5.3.4</A> for the detail. | |
115 | ||
116 | <P> | |
117 | ||
118 | <DIV CLASS="navigation"> | |
119 | <div class='online-navigation'> | |
120 | <p></p><hr /> | |
121 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
122 | <tr> | |
123 | <td class='online-navigation'><a rel="prev" title="5.2.4 List displays" | |
124 | href="lists.html"><img src='../icons/previous.png' | |
125 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
126 | <td class='online-navigation'><a rel="parent" title="5.2 Atoms" | |
127 | href="atoms.html"><img src='../icons/up.png' | |
128 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
129 | <td class='online-navigation'><a rel="next" title="5.2.6 Dictionary displays" | |
130 | href="dict.html"><img src='../icons/next.png' | |
131 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
132 | <td align="center" width="100%">Python Reference Manual</td> | |
133 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
134 | href="contents.html"><img src='../icons/contents.png' | |
135 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
136 | <td class='online-navigation'><img src='../icons/blank.png' | |
137 | border='0' height='32' alt='' width='32' /></td> | |
138 | <td class='online-navigation'><a rel="index" title="Index" | |
139 | href="genindex.html"><img src='../icons/index.png' | |
140 | border='0' height='32' alt='Index' width='32' /></A></td> | |
141 | </tr></table> | |
142 | <div class='online-navigation'> | |
143 | <b class="navlabel">Previous:</b> | |
144 | <a class="sectref" rel="prev" href="lists.html">5.2.4 List displays</A> | |
145 | <b class="navlabel">Up:</b> | |
146 | <a class="sectref" rel="parent" href="atoms.html">5.2 Atoms</A> | |
147 | <b class="navlabel">Next:</b> | |
148 | <a class="sectref" rel="next" href="dict.html">5.2.6 Dictionary displays</A> | |
149 | </div> | |
150 | </div> | |
151 | <hr /> | |
152 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
153 | </DIV> | |
154 | <!--End of Navigation Panel--> | |
155 | <ADDRESS> | |
156 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
157 | </ADDRESS> | |
158 | </BODY> | |
159 | </HTML> |