Understanding Python's name resolution and scoping rules
This is a medium-difficulty Python scoping question that tests whether you understand how the interpreter resolves variable names across different namespaces. It's the kind of question that separates candidates who have written production Python from those who learned syntax in isolation.
To answer correctly, you need to trace through Python's LEGB rule—the order in which the interpreter searches for a name: Local scope, Enclosing scope, Global scope, and Built-in scope. The question rewards careful attention to where variables are defined, reassigned, or shadowed, and how function calls and nested scopes interact with those bindings.
- Local vs. global variable scope
- Nested function scope and closures
- Variable shadowing and name lookup order
- The
global and nonlocal keywords