summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2002-09-06 18:06:04 (GMT)
committerRaymond Hettinger <python@rcn.com>2002-09-06 18:06:04 (GMT)
commit44c42b9cf33fac19ef0785ba3be9fc05f0be4dec (patch)
tree4320f1499512c0ddfcabcd824d78ee9aedb9f03a /Doc
parent9ab7dd4d5b616cd9486d3a73a5e8320f8c913a1a (diff)
downloadcpython-44c42b9cf33fac19ef0785ba3be9fc05f0be4dec.zip
cpython-44c42b9cf33fac19ef0785ba3be9fc05f0be4dec.tar.gz
cpython-44c42b9cf33fac19ef0785ba3be9fc05f0be4dec.tar.bz2
Added a tutorial note and example regarding the scope of loop variables
in a list comprehension. Includes a justification and a comparision to regular for-loops. Closes SF bug 605047.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/tut/tut.tex13
1 files changed, 13 insertions, 0 deletions
diff --git a/Doc/tut/tut.tex b/Doc/tut/tut.tex
index 1990ab8..512dd6e 100644
--- a/Doc/tut/tut.tex
+++ b/Doc/tut/tut.tex
@@ -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}}