Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5c8d29c

Browse files
committed
SF patch #872326: generator expression implementation
(Contributed by Jiwon Seo.) Add genexps to the reference manual.
1 parent 2e829c0 commit 5c8d29c

1 file changed

Lines changed: 46 additions & 1 deletion

File tree

Doc/ref/ref5.tex

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ \section{Atoms\label{atoms}}
5555
{\token{identifier} | \token{literal} | \token{enclosure}}
5656
\production{enclosure}
5757
{\token{parenth_form} | \token{list_display}}
58-
\productioncont{| \token{dict_display} | \token{string_conversion}}
58+
\productioncont{| \token{generator_expression | \token{dict_display}}}
59+
\productioncont{| \token{string_conversion}}
5960
\end{productionlist}
6061

6162

@@ -193,6 +194,48 @@ \subsection{List displays\label{lists}}
193194
\indexii{empty}{list}
194195

195196

197+
\subsection{Generator expressions\label{genexpr}}
198+
\indexii{generator}{expression}
199+
200+
A generator expression is a compact generator notation in parentheses:
201+
202+
\begin{productionlist}
203+
\production{generator_expression}
204+
{"(" \token{test} \token{genexpr_for} ")"}
205+
\production{genexpr_for}
206+
{"for" \token{expression_list} "in" \token{test}
207+
[\token{genexpr_iter}]}
208+
\production{genexpr_iter}
209+
{\token{genexpr_for} | \token{genexpr_if}}
210+
\production{genexpr_if}
211+
{"if" \token{test} [\token{genexpr_iter}]}
212+
\end{productionlist}
213+
214+
A generator expression yields a new generator object.
215+
\obindex{generator}
216+
\obindex{generator expression}
217+
It consists of a single expression followed by at least one
218+
\keyword{for} clause and zero or more \keyword{for} or \keyword{if}
219+
clauses. The iterating values of the new generator are those that
220+
would be produced by considering each of the \keyword{for} or
221+
\keyword{if} clauses a block, nesting from left to right, and
222+
evaluating the expression to yield a value that is reached the
223+
innermost block for each iteration.
224+
225+
Variables used in the generator expression are evaluated lazily
226+
when the \method{next()} method is called for generator object
227+
(in the same fashion as normal generators). However, the leftmost
228+
\keyword{for} clause is immediately evaluated so that error produced
229+
by it can be seen before any other possible error in the code that
230+
handles the generator expression.
231+
Subsequent \keyword{for} clauses cannot be evaluated immediately since
232+
they may depend on the previous \keyword{for} loop.
233+
For example: \samp{(x*y for x in range(10) for y in bar(x))}.
234+
235+
The parentheses can be omitted on calls with only one argument.
236+
See section \ref{calls} for the detail.
237+
238+
196239
\subsection{Dictionary displays\label{dict}}
197240
\indexii{dictionary}{display}
198241

@@ -432,6 +475,8 @@ \subsection{Calls\label{calls}}
432475
\begin{productionlist}
433476
\production{call}
434477
{\token{primary} "(" [\token{argument_list} [","]] ")"}
478+
{\token{primary} "(" [\token{argument_list} [","] |
479+
\token{test} \token{genexpr_for} ] ")"}
435480
\production{argument_list}
436481
{\token{positional_arguments} ["," \token{keyword_arguments}]}
437482
\productioncont{ ["," "*" \token{expression}]}

0 commit comments

Comments
 (0)