What this Python scoping and namespace question tests
This is a medium-difficulty Python question that probes your understanding of local scope and how the locals() function behaves within function execution. It's the kind of question interviewers use to assess whether you have a solid grasp of Python's name-binding mechanics — knowledge that matters when debugging unexpected variable behaviour or reasoning about code that manipulates its own namespace.
To answer correctly, you need to understand the timing of when locals() captures the current scope, how assignments interact with the local namespace dictionary, and what gets reflected in that dictionary at different points in execution. The question rewards careful reading of Python's scoping rules and awareness of how built-in introspection functions report on live state.
- Local vs. global scope and the LEGB rule
- The behaviour of
locals() inside function bodies - How variable assignments update the local namespace
- Distinction between the namespace dictionary and actual variable binding