]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#21739: mention subtle difference between loops and listcomps in tutorial.
authorR David Murray <rdmurray@bitdance.com>
Wed, 1 Oct 2014 01:25:38 +0000 (21:25 -0400)
committerR David Murray <rdmurray@bitdance.com>
Wed, 1 Oct 2014 01:25:38 +0000 (21:25 -0400)
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'.

Doc/tutorial/datastructures.rst

index f2b66f70d830be1b118d039a535875475981c789..5c3ae1698012f7c7c63e38a1ad611bcf992c1c20 100644 (file)
@@ -199,12 +199,17 @@ For example, assume we want to create a list of squares, like::
    >>> 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`