Programming Search⌘K

Featureadvanced

Comprehension scope

Also called comprehension variable leak, loop variable in a comprehension, walrus in a comprehension, comprehension in a class body, inlined comprehensions, PEP 709.

A comprehension runs in its own nested scope, so its loop variables do not leak. The leftmost iterable is evaluated in the enclosing scope, and a := target binds in the containing scope.

Language
Python

Synopsis

x = "old value"
squares = [x ** 2 for x in range(4)]
print(x, squares)
Output
old value [0, 1, 4, 9]

The x inside the comprehension is a different variable from the x outside it.

Semantics

r1. "Aside from the iterable expression in the leftmost for clause, the comprehension is executed in a separate implicitly nested scope. This ensures that names assigned to in the target list don’t “leak” into the enclosing scope."1 Example: no-leak.

r2. "The iterable expression in the leftmost for clause is evaluated directly in the enclosing scope and then passed as an argument to the implicitly nested scope."1 For a generator expression, "The iterable expression in the leftmost for clause is evaluated immediately".1 Example: leftmost.

r3. An assignment expression in a comprehension "binds the target in the containing scope, honoring a nonlocal or global declaration for the target in that scope, if one exists."3 Example: witness.

r4. "Assignment expressions are not allowed inside comprehension iterable expressions (that is, the expressions after the in keyword), nor anywhere within comprehensions that appear directly in a class definition."1 Example: walrus-iterable.

r5. "The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods. This includes comprehensions and generator expressions".2 Only the leftmost iterable can use a name from the class body (rule r2). Examples: class-body, class-name.

r6. PEP 709 changed how CPython runs list, set and dict comprehensions. It proposes "to inline list, dictionary, and set comprehensions into the code where they are defined, and provide the expected isolation by pushing/popping clashing locals on the stack."4 The rules r1 to r5 stay the same. "Generator expressions are currently not inlined in the reference implementation of this PEP."4

Examples

nums = [1, 2]
gen = (n * 10 for n in nums)
nums = [7, 8]
print(list(gen))
Output
[10, 20]

The generator got the first list when it was made (rule r2). A later change to the name nums has no effect.

lines = ["code", "# note", "more code"]
if any((comment := line).startswith("#") for line in lines):
    print("First comment:", comment)

total = 0
partial_sums = [total := total + v for v in [1, 2, 3]]
print(partial_sums, total)
Output
First comment: # note
[1, 3, 6] 6

Both examples are adapted from PEP 572. The name after := is set in the enclosing scope, so the program can read it after the comprehension (rule r3).

class Grid:
    size = 3
    cells = [i * 2 for i in range(size)]

print(Grid.cells)
Output
[0, 2, 4]

size is in the leftmost iterable, so it is evaluated in the class body (rule r2).

Pitfalls

Other parts of a comprehension in a class body cannot see the class names:

This code raises NameError, on purpose.

class A:
    a = 42
    b = list(a + i for i in range(10))

The execution model reference gives this example as one that fails (rule r5). Move the value into the leftmost iterable, or compute it outside the class.

An assignment expression cannot be in the iterable part:

This code raises SyntaxError, on purpose.

result = [i + 1 for i in (j := [1, 2, 3])]

Assign the list in a separate statement first (rule r4).

Comparison

A Go for statement with a range clause declares its iteration variables with :=. The Go spec says: "The iteration variables may be declared by the "range" clause using a form of short variable declaration (:=). In this case their scope is the block of the "for" statement".5 So in both languages the loop variable does not exist after the loop. Go has no comprehensions and no rule like r3.

References

  1. The Python Language Reference, Python 3.15, 6.2.6. Comprehensions; 6.2.7. Generator expressions
  2. The Python Language Reference, 4. Execution model, 4.2.2. Resolution of names
  3. PEP 572: Assignment Expressions, Scope of the target
  4. PEP 709: Inlined comprehensions, Abstract; Specification
  5. The Go Programming Language Specification, For statements with range clause

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source