What this Python name-binding interview question tests
This is a medium-difficulty Python question that probes your understanding of scope, name resolution, and the order in which Python evaluates expressions. It is typical of screening rounds where interviewers want to see whether you can trace code execution carefully and predict output without running it.
To solve problems like this, you need to understand how Python resolves names in the local, enclosing, global, and built-in scopes (the LEGB rule), how assignment statements work, and the precise timing of when names become bound. Many candidates skip over subtle ordering details—especially around when a name is defined versus when it is referenced. Attention to these mechanics is valuable both in interviews and when debugging real code.
- Scope and the LEGB lookup order
- Name binding and the assignment statement
- When names are resolved during expression evaluation