Index: Doc/ref/ref5.tex =================================================================== RCS file: /cvsroot/python/python/dist/src/Doc/ref/ref5.tex,v retrieving revision 1.79 diff -c -r1.79 ref5.tex *** Doc/ref/ref5.tex 9 Nov 2003 16:33:56 -0000 1.79 --- Doc/ref/ref5.tex 17 Mar 2004 05:24:05 -0000 *************** *** 55,61 **** {\token{identifier} | \token{literal} | \token{enclosure}} \production{enclosure} {\token{parenth_form} | \token{list_display}} ! \productioncont{| \token{dict_display} | \token{string_conversion}} \end{productionlist} --- 55,62 ---- {\token{identifier} | \token{literal} | \token{enclosure}} \production{enclosure} {\token{parenth_form} | \token{list_display}} ! \productioncont{| \token{dict_display} | \token{generator_expression}} ! \productioncont{| \token{string_conversion}} \end{productionlist} *************** *** 222,227 **** --- 223,260 ---- \indexii{immutable}{object} + \subsection{Generator expressions\label{genexpr}} + \indexii{generator}{expression} + + A generator expression is a compact generator notation in parentheses: + + \begin{productionlist} + \production{generator_expression} + {"(" \token{test} \token{genexpr_for} ")"} + \production{genexpr_for} + {"for" \token{expression_list} "in" \token{test} + [\token{genexpr_iter}]} + \production{genexpr_iter} + {\token{genexpr_for} | \token{genexpr_if}} + \production{genexpr_if} + {"if" \token{test} [\token{genexpr_iter}]} + \end{productionlist} + + A generator expression yields a new generator object. + \obindex{generator} + \obindex{generator expression} + It consists of a single expression followed by at least one + \keyword{for} clause and zero or more \keyword{for} or \keyword{if} + clauses. The iterating values of the new generator are those that + would be produced by considering each of the \keyword{for} or + \keyword{if} clauses a block, nesting from left to right, and + evaluating the expression to yield a value that is reached the + innermost block for each iteration. + + The parentheses can be omitted on calls with only one argument. + See section \ref{calls} for the detail. + + \subsection{String conversions\label{string-conversions}} \indexii{string}{conversion} \indexii{reverse}{quotes} *************** *** 427,433 **** \begin{productionlist} \production{call} ! {\token{primary} "(" [\token{argument_list} [","]] ")"} \production{argument_list} {\token{positional_arguments} ["," \token{keyword_arguments}]} \productioncont{ ["," "*" \token{expression}]} --- 460,467 ---- \begin{productionlist} \production{call} ! {\token{primary} "(" [ \token{argument_list} [","] | ! \token{test} \token{genexpr_for} ] ")"} \production{argument_list} {\token{positional_arguments} ["," \token{keyword_arguments}]} \productioncont{ ["," "*" \token{expression}]} Index: Doc/tut/tut.tex =================================================================== RCS file: /cvsroot/python/python/dist/src/Doc/tut/tut.tex,v retrieving revision 1.224 diff -c -r1.224 tut.tex *** Doc/tut/tut.tex 24 Feb 2004 16:13:36 -0000 1.224 --- Doc/tut/tut.tex 17 Mar 2004 05:24:06 -0000 *************** *** 4396,4401 **** --- 4396,4438 ---- In combination, these features make it easy to create iterators with no more effort than writing a regular function. + In case that thing to do in a generator is simple, you can write + it even more concise using generator expression. It is somewhat + simliar to list comprehensions but it yields not a list but a + generator producing result values instead. Each generator expression + consists of an expression followed by a for clause, then zero or + more for or if clauses. An example shows that generator expressions + can be trivially easy to create: + + \begin{verbatim} + >>> sum(x**2 for x in range(10)) + 285 + + >>> min(ch.upper() for ch in 'They killed Kenny!' if ch.islower()) + 'D' + + >>> data = 'live' + >>> '/'.join(data[index] for index in range(len(data)-1, -1, -1)) + 'e/v/i/l' + + >>> (x for x in range(100) if x**2 > 1000).next() + 32 + + >>> g = ('%02x' % ord(c) for c in 'hello') + >>> g.next(); g.next(); g.next(); + '68' + '65' + '6c' + + >>> g = ((x, y, x*y) for x in range(2) for y in range(2)) + >>> for v in g: + ... print v + ... + (0, 0, 0) + (0, 1, 0) + (1, 0, 0) + (1, 1, 1) + \end{verbatim} \chapter{Brief Tour of the Standard Library \label{briefTour}}