Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, list comprehensions "leak" their variable in Python 2.x, just like for loops.</p> <p>In retrospect, this was recognized to be a mistake, and it was avoided with generator expressions. EDIT: As <a href="https://stackoverflow.com/questions/4198906/python-list-comprehension-rebind-names-even-after-scope-of-comprehension-is-thi/4199344#comment10208805_4199355">Matt B. notes</a> it was also avoided when set and dictionary comprehension syntaxes were backported from Python 3.</p> <p>List comprehensions' behavior had to be left as it is in Python 2, but it's fully fixed in Python 3.</p> <p>This means that in all of:</p> <pre><code>list(x for x in a if x&gt;32) set(x//4 for x in a if x&gt;32) # just another generator exp. dict((x, x//16) for x in a if x&gt;32) # yet another generator exp. {x//4 for x in a if x&gt;32} # 2.7+ syntax {x: x//16 for x in a if x&gt;32} # 2.7+ syntax </code></pre> <p>the <code>x</code> is always local to the expression while these:</p> <pre><code>[x for x in a if x&gt;32] set([x//4 for x in a if x&gt;32]) # just another list comp. dict([(x, x//16) for x in a if x&gt;32]) # yet another list comp. </code></pre> <p>in Python 2.x all leak the <code>x</code> variable to the surrounding scope.</p> <hr> <p><strong>UPDATE for Python 3.8(?)</strong>: <a href="https://www.python.org/dev/peps/pep-0572/" rel="nofollow noreferrer">PEP 572</a> will introduce <code>:=</code> assignment operator that <strong>deliberately leaks</strong> out of comprehensions and generator expressions! It's motivated by essentially 2 use cases: capturing a "witness" from early-terminating functions like <code>any()</code> and <code>all()</code>:</p> <pre><code>if any((comment := line).startswith('#') for line in lines): print("First comment:", comment) else: print("There are no comments") </code></pre> <p>and updating mutable state:</p> <pre><code>total = 0 partial_sums = [total := total + v for v in values] </code></pre> <p>See <a href="https://www.python.org/dev/peps/pep-0572/#appendix-b-rough-code-translations-for-comprehensions" rel="nofollow noreferrer">Appendix B</a> for exact scoping. The variable is assigned in closest surrounding <code>def</code> or <code>lambda</code>, unless that function declares it <code>nonlocal</code> or <code>global</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload