Name Resolution in Practice
When you write Python code, the interpreter must determine what each variable name refers to every time it is used. This process is called name resolution, and it follows a specific order known as the LEGB rule: local, enclosing, global, and built-in. In practice, this means Python first looks for a variable in the current function or block (local), then in any enclosing functions (enclosing), then at the module level (global), and finally in Pythonβs built-in names (built-in).
Understanding how name resolution works is essential for debugging, because scope-related errors are common. For example, if you try to use a variable before it is assigned in the local scope, Python will raise an UnboundLocalError. This error message typically says that a local variable is referenced before assignment. To debug such issues, carefully check where your variables are assigned and whether you need to use global or nonlocal to access variables from outer scopes.
12345678910111213141516# Example: UnboundLocalError and fixing it with 'nonlocal' def outer(): x = 10 def inner(): # Uncommenting the next line will cause UnboundLocalError: # print(x) # x = x + 1 # Instead, declare 'nonlocal' to modify 'x' from the outer scope: nonlocal x x = x + 1 print(x) inner() outer() # Output: 11
To write robust Python code, you should follow best practices for variable naming and scope management. Always use clear, descriptive names for variables to avoid confusion between local and global names. Avoid reusing variable names in nested scopes unless necessary, and use global or nonlocal declarations only when you are certain you need to modify variables outside the current scope. Keeping variable lifetimes short and limiting their scope makes your code easier to read and less prone to bugs caused by accidental shadowing or unintended side effects.
123456789101112# Example: Safe variable usage and clear naming def calculate_total(prices): total = 0 for price in prices: total += price return total item_prices = [10, 20, 30] order_total = calculate_total(item_prices) print("Order total:", order_total) # Output: Order total: 60
1. What causes an UnboundLocalError?
2. How can you avoid scope-related bugs in Python?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain the difference between global and nonlocal in Python?
What are some common mistakes to avoid with variable scopes?
Can you give more examples of scope-related errors and how to fix them?
Awesome!
Completion rate improved to 8.33
Name Resolution in Practice
Swipe to show menu
When you write Python code, the interpreter must determine what each variable name refers to every time it is used. This process is called name resolution, and it follows a specific order known as the LEGB rule: local, enclosing, global, and built-in. In practice, this means Python first looks for a variable in the current function or block (local), then in any enclosing functions (enclosing), then at the module level (global), and finally in Pythonβs built-in names (built-in).
Understanding how name resolution works is essential for debugging, because scope-related errors are common. For example, if you try to use a variable before it is assigned in the local scope, Python will raise an UnboundLocalError. This error message typically says that a local variable is referenced before assignment. To debug such issues, carefully check where your variables are assigned and whether you need to use global or nonlocal to access variables from outer scopes.
12345678910111213141516# Example: UnboundLocalError and fixing it with 'nonlocal' def outer(): x = 10 def inner(): # Uncommenting the next line will cause UnboundLocalError: # print(x) # x = x + 1 # Instead, declare 'nonlocal' to modify 'x' from the outer scope: nonlocal x x = x + 1 print(x) inner() outer() # Output: 11
To write robust Python code, you should follow best practices for variable naming and scope management. Always use clear, descriptive names for variables to avoid confusion between local and global names. Avoid reusing variable names in nested scopes unless necessary, and use global or nonlocal declarations only when you are certain you need to modify variables outside the current scope. Keeping variable lifetimes short and limiting their scope makes your code easier to read and less prone to bugs caused by accidental shadowing or unintended side effects.
123456789101112# Example: Safe variable usage and clear naming def calculate_total(prices): total = 0 for price in prices: total += price return total item_prices = [10, 20, 30] order_total = calculate_total(item_prices) print("Order total:", order_total) # Output: Order total: 60
1. What causes an UnboundLocalError?
2. How can you avoid scope-related bugs in Python?
Thanks for your feedback!