QuestionQ7

Python/PyEZ

Which statement is correct about Python variables that are defined in the global namespace?

A. Global variables are stored in disk storage and can be used by any Python script executed on this device.

B. Global variables can only be referenced in the module's code within the global namespace.

C. Global variables must have the data type explicitly declared.

D. Global variables can be referenced in any portion of the module's code.

  • A Global variables are stored in disk storage and can be used by any Python script executed on this device.
  • B Global variables can only be referenced in the module’s code within the global namespace.
  • C Global variables must have the data type explicitly declared.
  • D Global variables can be referenced in any portion of the module’s code.
Explanation

Variables assigned at the top level of a Python module become part of that module's global namespace and are held in memory for the lifetime of the module, not written to disk, and are not restricted to other scripts. Because of Python's LEGB (Local, Enclosing, Global, Built-in) name resolution rule, code anywhere within that module — including inside functions and classes — can read a global variable's value, and can also modify it if the function explicitly declares it with the 'global' keyword. Python variables also do not require explicit type declarations, since the language uses dynamic typing. This makes the statement that global variables can be referenced anywhere within the module's code the accurate one.

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!