local variable
In Python, a local variable is a name that you assign inside a function or method body, including function parameters.
Local variables are accessible within that function’s block, including any nested functions. They usually live only for the duration of the function call, unless a closure keeps them alive. Python resolves names using the LEGB rule, which searches the local, enclosing, global, and built-in scopes in that order. As a result, local variables shadow names in outer scopes.
Inside a function, any assignment makes the target name local to the function block unless you use the global or nonlocal keyword.
Example
Basic local variable and visibility:
>>> def greet(name):
... message = f"Hello, {name}!" # Define a local variable, message
... return message
...
>>> greet("Pythonista")
'Hello, Pythonista!'
>>> message
Traceback (most recent call last):
...
NameError: name 'message' is not defined
Assignment makes a name local to a function:
>>> x = 10 # Global x
>>> def assign_global():
... print(x) # Tries to read 'x'
... x = 42 # Makes 'x' local
...
>>> assign_global()
Traceback (most recent call last):
...
UnboundLocalError: cannot access local variable 'x' where it is not associated with a value
Use global to rebind a global name:
>>> x = 10
>>> def assign_global():
... global x
... print(x) # Reads the global 'x'
... x = 42 # Rebinds the global 'x'
...
>>> assign_global()
10
>>> x
42
Use nonlocal to rebind names from the enclosing scope:
>>> def make_counter():
... count = 0 # Defines a local name
... def inc():
... nonlocal count # Refers to the enclosing 'count'
... count += 1
... return count
... return inc
...
>>> counter = make_counter()
>>> counter()
1
>>> counter()
2
Related Resources
Tutorial
Python Scope and the LEGB Rule: Resolving Names in Your Code
Understanding Python's variable scope and the LEGB rule helps you avoid name collisions and unexpected behavior. Learn to manage scope and write better code.
For additional information on related topics, take a look at the following resources:
- Using and Creating Global Variables in Your Python Functions (Tutorial)
- The LEGB Rule & Understanding Python Scope (Course)
- Variables in Python: Usage and Best Practices (Tutorial)
- Python Closures: Common Use Cases and Examples (Tutorial)
- Defining Your Own Python Function (Tutorial)
- Namespaces in Python (Tutorial)
- Python Scope and the LEGB Rule: Resolving Names in Your Code (Quiz)
- Working With Global Variables in Python Functions (Course)
- Using and Creating Global Variables in Your Python Functions (Quiz)
- Variables in Python (Course)
- Variables in Python: Usage and Best Practices (Quiz)
- Exploring Python Closures: Examples and Use Cases (Course)
- Python Closures: Common Use Cases and Examples (Quiz)
- Defining and Calling Python Functions (Course)
- Defining and Calling Python Functions (Quiz)
- Defining Your Own Python Function (Quiz)
- Navigating Namespaces and Scope in Python (Course)
- Namespaces in Python (Quiz)
- Namespaces and Scope in Python (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Oct. 11, 2026