LEGB Rules
URL: Namespaces and Scope in Python – Real Python
In a Python program, there are four types of namespaces: (Searched in sequence)
- Local: If you refer to x inside a function, then the interpreter first searches for it in the innermost scope that’s local to that function.
- Enclosing: If x isn’t in the local scope but appears in a function that resides inside another function, then the interpreter searches in the enclosing function’s scope.
- Global: If neither of the above searches is fruitful, then the interpreter looks in the global scope next.
- Built-in: If it can’t find x anywhere else, then the interpreter tries the built-in scope.
This is the LEGB rule as it’s commonly called in Python literature (although the term doesn’t actually appear in the Python documentation). The interpreter searches for a name from the inside out, looking in the local, enclosing, global, and finally the built-in scope
Global VS Nonlocal
- Global
Access var defined outside function - Nonlocal
Access var defined in closure function
Example
nonlocal
works in nested structure