Python Developer interview question · Most asked
Explain LEGB scoping in Python.
Summary
LEGB is Python's name lookup order: Local, Enclosing, Global, then Built-in scope.
Longer Answer
When Python resolves a name, it first checks the current function or comprehension local scope, then enclosing function scopes for closures, then the module global scope, and finally builtins. Assignment normally creates a local name unless global or nonlocal is declared.
Interviewers look for the practical consequences: late binding in closures, UnboundLocalError when assigning to a name that was expected to be global, and when nonlocal is appropriate for nested functions.
Key Points
- When Python resolves a name, it first checks the current function or comprehension local scope, then enclosing function scopes for closures, then the module global scope, and finally builtins
- Assignment normally creates a local name unless global or nonlocal is declared
- Interviewers look for the practical consequences: late binding in closures, UnboundLocalError when assigning to a name that was expected to be global, and when nonlocal is appropriate for nested functions
- For 'Explain LEGB scoping in Python.', a strong interview answer should define the mechanism, name the common failure mode, and explain the production decision it affects.
Interviewers look for the practical consequences: late binding in closures, UnboundLocalError when assigning to a name that was expected to be global, and when nonlocal is appropriate for nested functions
Comparison / Tradeoffs
| Aspect | Notes |
|---|---|
| Core idea | LEGB is Python's name lookup order: Local, Enclosing, Global, then Built-in scope. |
| Practical tradeoff | For 'Explain LEGB scoping in Python.', a strong interview answer should define the mechanism, name the common failure mode, and explain the production decision it affects. |
Interviewers look for the practical consequences: late binding in closures, UnboundLocalError when assigning to a name that was expected to be global, and when nonlocal is appropriate for nested functions. |
Explanation
Summary
LEGB is Python's name lookup order: Local, Enclosing, Global, then Built-in scope.
Longer Answer
For 'Explain LEGB scoping in Python.', a strong interview answer should define the mechanism, name the common failure mode, and explain the production decision it affects.
Interviewers look for the practical consequences: late binding in closures, UnboundLocalError when assigning to a name that was expected to be global, and when nonlocal is appropriate for nested functions.
Key Points
- For 'Explain LEGB scoping in Python.', a strong interview answer should define the mechanism, name the common failure mode, and explain the production decision it affects
- Interviewers look for the practical consequences: late binding in closures, UnboundLocalError when assigning to a name that was expected to be global, and when nonlocal is appropriate for nested functions
- Relevant areas: Python, Core
- Use the answer to connect the concept back to a realistic production or interview scenario for 'Explain LEGB scoping in Python.'
Comparison / Tradeoffs
| Aspect | Notes |
|---|---|
| Core idea | LEGB is Python's name lookup order: Local, Enclosing, Global, then Built-in scope. |
| Practical tradeoff | In an interview, relate 'Explain LEGB scoping in Python.' to implementation decisions, tradeoffs, and operational impact. |
More most-asked Python Developer questions
- Backend Python interview: explain N+1 queries.
- Backend Python interview: explain authN vs authZ.
- Backend Python interview: explain SQL injection prevention.
- Explain decorators in Python.
- Explain context managers in Python.
- Explain GIL in Python.
- Explain threading vs multiprocessing vs asyncio in Python.
- Explain generators in Python.