mirror of
https://github.com/python/cpython.git
synced 2025-08-03 00:23:06 +00:00
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.
This commit is contained in:
parent
9ab7dd4d5b
commit
44c42b9cf3
1 changed files with 13 additions and 0 deletions
|
@ -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}}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue