From: Raymond Hettinger Date: Fri, 6 Sep 2002 18:09:22 +0000 (+0000) Subject: Added a tutorial note and example regarding the scope of loop variables X-Git-Tag: v2.2.2b1~182 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=4f976188454765080cf03d4d2ebfd4d58cca9573;p=thirdparty%2FPython%2Fcpython.git 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. --- diff --git a/Doc/tut/tut.tex b/Doc/tut/tut.tex index 5c1e94b994a3..444e7d97c614 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}}