[Python-checkins] cpython (3.4): #21739: mention subtle difference between loops and listcomps in tutorial.
r.david.murray
python-checkins at python.org
Wed Oct 1 03:27:39 CEST 2014
https://hg.python.org/cpython/rev/84895d037258
changeset: 92703:84895d037258
branch: 3.4
parent: 92699:ec6cee80926f
user: R David Murray <rdmurray at bitdance.com>
date: Tue Sep 30 21:25:38 2014 -0400
summary:
#21739: mention subtle difference between loops and listcomps in tutorial.
We don't want to go into a full explanation of scopes at this point in the
tutorial, so we just mention that the loop creates or overwrites a persistent
variable while the listcomp doesn't. Not mentioning this would lead someone
to incorrectly assume loops and listcomps were *completely* equivalent, which
would confuse them later.
Original patch by Rose Ames, tweaked to remove the word 'scope'.
files:
Doc/tutorial/datastructures.rst | 11 ++++++++---
1 files changed, 8 insertions(+), 3 deletions(-)
diff --git a/Doc/tutorial/datastructures.rst b/Doc/tutorial/datastructures.rst
--- a/Doc/tutorial/datastructures.rst
+++ b/Doc/tutorial/datastructures.rst
@@ -199,12 +199,17 @@
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
-We can obtain the same result with::
+Note that this creates (or overwrites) a variable named ``x`` that still exists
+after the loop completes. We can calculate the list of squares without any
+side effects using::
+
+ squares = list(map(lambda x: x**2, range(10)))
+
+or, equivalently::
squares = [x**2 for x in range(10)]
-This is also equivalent to ``squares = list(map(lambda x: x**2, range(10)))``,
-but it's more concise and readable.
+which is more concise and readable.
A list comprehension consists of brackets containing an expression followed
by a :keyword:`for` clause, then zero or more :keyword:`for` or :keyword:`if`
--
Repository URL: https://hg.python.org/cpython
More information about the Python-checkins
mailing list