Understanding Python's globals() function and namespace access
This is an easy Python question that tests your grasp of how Python manages the global namespace and what the globals() built-in returns at runtime. It's a useful conceptual check for anyone writing or debugging Python code, especially in environments where dynamic variable lookup matters.
The question asks you to trace through a code snippet and predict what gets printed—or whether an error occurs instead. To answer it correctly, you need to understand what globals() actually is, how it relates to module scope, and when you might encounter a NameError or other runtime exception. This kind of reasoning is particularly relevant when working with introspection, metaprogramming, or dynamic imports.
- The distinction between the global namespace and the globals() dictionary
- How variable binding works at module scope
- When name lookups fail and exceptions are raised