Improved wording for generator expressions.

This commit is contained in:
Raymond Hettinger 2004-05-19 22:30:25 +00:00
parent 059e170c1d
commit 6e1fd2f208
1 changed files with 15 additions and 22 deletions

View File

@ -14,8 +14,8 @@
\maketitle \maketitle
\tableofcontents \tableofcontents
This article explains the new features in Python 2.4. No release date This article explains the new features in Python 2.4. The release
for Python 2.4 has been set; expect that this will happen mid-2004. date is expected to be around September 2004.
While Python 2.3 was primarily a library development release, Python While Python 2.3 was primarily a library development release, Python
2.4 may extend the core language and interpreter in 2.4 may extend the core language and interpreter in
@ -91,12 +91,12 @@ XXX write this.
%====================================================================== %======================================================================
\section{PEP 229: Generator Expressions} \section{PEP 229: Generator Expressions}
Generator expressions create in-line generators using a syntax similar Now, simple generators can be coded succinctly as expressions using a syntax
to list comprehensions but with parenthesis instead of the surrounding like list comprehensions but with parentheses instead of brackets. These
brackets. expressions are designed for situations where the generator is used right
away by an enclosing function. Generator expressions are more compact but
Genexps allow simple generators to be constructed without a separate function less versatile than full generator definitions and the tend to be more memory
definition. Writing: friendly than equivalent list comprehensions.
\begin{verbatim} \begin{verbatim}
g = (tgtexp for var1 in exp1 for var2 in exp2 if exp3) g = (tgtexp for var1 in exp1 for var2 in exp2 if exp3)
@ -121,11 +121,9 @@ a whole list is memory all at once. Applications using memory
friendly generator expressions may scale-up to high volumes of data friendly generator expressions may scale-up to high volumes of data
more readily than with list comprehensions. more readily than with list comprehensions.
Generator expressions are intended to be used inside functions Generator expressions are best used in functions that consume their
such as \function{sum()}, \function{min()}, \function{set()}, and data all at once and would not benefit from having a full list instead
\function{dict()}. These functions consume their data all at once of a generator as an input:
and would not benefit from having a full list instead of a generator
as an input:
\begin{verbatim} \begin{verbatim}
>>> sum(i*i for i in range(10)) >>> sum(i*i for i in range(10))
@ -149,20 +147,15 @@ as an input:
\end{verbatim} \end{verbatim}
These examples show the intended use for generator expressions
in situations where the values get consumed immediately after the
generator is created. In these situations, they operate like
memory efficient versions of list comprehensions.
For more complex uses of generators, it is strongly recommended that For more complex uses of generators, it is strongly recommended that
the traditional full generator definitions be used instead. In a the traditional full generator definitions be used instead. In a
generator expression, the first for-loop expression is evaluated generator expression, the first for-loop expression is evaluated
as soon as the expression is defined while the other expressions do as soon as the expression is defined while the other expressions do
not get evaluated until the generator is run. This nuance is never not get evaluated until the generator is run. This nuance is never
an issue when the generator is used immediately. If it is not used an issue when the generator is used immediately; however, if it is not
right away, then it is better to write a full generator definition used right away, a full generator definition would be much more clear
which more clearly reveals when the expressions are evaluated and is about when the sub-expressions are evaluated and would be more obvious
more obvious about the visibility and lifetime of its looping variables. about the visibility and lifetime of the variables.
\begin{seealso} \begin{seealso}
\seepep{289}{Generator Expressions}{Proposed by Raymond Hettinger and \seepep{289}{Generator Expressions}{Proposed by Raymond Hettinger and