Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
6e1fd2f2
Commit
6e1fd2f2
authored
May 19, 2004
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improved wording for generator expressions.
parent
059e170c
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
22 deletions
+15
-22
Doc/whatsnew/whatsnew24.tex
Doc/whatsnew/whatsnew24.tex
+15
-22
No files found.
Doc/whatsnew/whatsnew24.tex
View file @
6e1fd2f2
...
@@ -14,8 +14,8 @@
...
@@ -14,8 +14,8 @@
\maketitle
\maketitle
\tableofcontents
\tableofcontents
This article explains the new features in Python 2.4.
No release dat
e
This article explains the new features in Python 2.4.
The releas
e
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.
...
@@ -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
...
@@ -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:
...
@@ -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 i
s
about when the sub-expressions are evaluated and would be more obviou
s
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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment