Understanding variable storage and scope in Python
This is a medium-difficulty Python question that tests your grasp of how Python manages memory and variable scope. It's the kind of foundational question that interviewers ask to separate candidates who understand Python's runtime model from those who rely on pattern matching.
To answer this correctly, you need to understand Python's execution model: where variables live during program execution, how scope affects visibility and lifetime, and the distinction between local, nonlocal, global, and built-in namespaces. The specific answer depends on the context in which the variable appears—whether it's in a function, a class, a module, or within nested scopes.
- Local vs. global scope and the LEGB rule
- The role of the stack frame in function execution
- How Python's namespace hierarchy resolves name lookups
- Lifetime and garbage collection