]> granicus.if.org Git - python/commitdiff
Added a tutorial note and example regarding the scope of loop variables
authorRaymond Hettinger <python@rcn.com>
Fri, 6 Sep 2002 18:06:04 +0000 (18:06 +0000)
committerRaymond Hettinger <python@rcn.com>
Fri, 6 Sep 2002 18:06:04 +0000 (18:06 +0000)
in a list comprehension.  Includes a justification and a comparision
to regular for-loops.

Closes SF bug 605047.

Doc/tut/tut.tex

index 1990ab853415a9a16fff1bf24e208f38209738fb..512dd6e64be0bf8daf7316ef6d01ccc289a642f1 100644 (file)
@@ -1862,6 +1862,19 @@ SyntaxError: invalid syntax
 [8, 12, -54]
 \end{verbatim}
 
+To make list comprehensions match the behavior of \keyword{for}
+loops, assignments to the loop variable remain visible outside
+of the comprehension:
+
+\begin{verbatim}
+>>> x = 100                     # this gets overwritten
+>>> [x**3 for x in range(5)]
+[0, 1, 8, 27, 64]
+>>> x
+4                               # the final value for range(5)
+>>
+\end{verbatim}
+
 
 \section{The \keyword{del} statement \label{del}}