Featureadvanced
Name resolution
Also called how Python looks up a name, UnboundLocalError, NameError, class scope and comprehensions, Python name lookup rules, nearest enclosing scope.
Python resolves a name in the nearest enclosing scope that binds it. An unknown name raises NameError, and a local name used before its binding raises UnboundLocalError. Class bodies are special.
- Language
- Python
Synopsis
x = "global"
def outer():
x = "outer"
def middle():
def inner():
return x
return inner()
return middle()
print(outer())
outer
inner and middle do not bind x, so Python uses the binding of the nearest enclosing function that binds it.
Semantics
r1. "A scope defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name."1 Example: nearest.
r2. "When a name is used in a code block, it is resolved using the nearest enclosing scope. The set of all such scopes visible to a code block is called the block's environment."1 Example: nearest.
r3. "When a name is not found at all, a NameError exception is raised." If a local variable "has not yet been bound to a value at the point where the name is used, an UnboundLocalError exception is raised. UnboundLocalError is a subclass of NameError."1 Examples: unknown-name, used-before-binding.
r4. "If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block." "Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations."1 Example: used-before-binding.
r5. With a global statement, names "are resolved in the top-level namespace by searching the global namespace, i.e. the namespace of the module containing the code block, and the builtins namespace". "The global namespace is searched first."1 "If the nearest enclosing scope for a free variable contains a global statement, the free variable is treated as a global."1 Example: global-in-enclosing.
r6. In a class body, name resolution has "an exception that unbound local variables are looked up in the global namespace."1 Example: class-lookup.
r7. "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".1 Example: class-genexp.
Examples
This code raises NameError, on purpose.
print(undefined_name)
print(issubclass(UnboundLocalError, NameError))
True
x = "global"
def outer():
global x
x = "set by outer"
def inner():
return x
return inner()
print(outer(), x)
set by outer set by outer
outer declares x global, so the free variable x of inner is the global x (rule r5).
x = "global"
def f():
x = "enclosing"
class C:
print(x)
x = "class"
return C.x
print(f())
global
class
The class body binds x, so x is local to the class block. At the first print(x), the local x is not bound yet, and Python looks in the global namespace. It skips the x of f (rule r6).
Pitfalls
An assignment later in a function makes a name local in the whole function:
This code raises UnboundLocalError, on purpose.
x = 10
def foo():
print(x)
x += 1
foo()
The FAQ explains: "when you make an assignment to a variable in a scope, that variable becomes local to that scope and shadows any similarly named variable in the outer scope."2
A generator expression in a class body cannot use the names of the class:
This code raises NameError, on purpose.
class A:
a = 42
b = list(a + i for i in range(10))
This is the example of the reference (rule r7). Bind the value in a function parameter, or compute it outside the class.
Comparison
In Go, "An identifier declared in a block may be redeclared in an inner block. While the identifier of the inner declaration is in scope, it denotes the entity declared by the inner declaration."3 So Go also uses the nearest declaration. But in Go, "Every identifier in a program must be declared", and the scope of a local variable begins after its declaration.3 In Python, a binding anywhere in a function makes the name local in the whole function, also before the binding (rule r4).
References
- The Python Language Reference, Python 3.15, 4.2.2. Resolution of names
- Programming FAQ, Python 3.15 documentation, Why am I getting an UnboundLocalError when the variable has a value?
- The Go Programming Language Specification, Declarations and scope
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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag 4PKDKS Edit this page History Report an error Markdown