Question 71
What is the value of __name__ when a Python script is executed directly from the command line?
When a file is run directly, Python sets __name__ = '__main__'. When imported as a module, __name__ is set to the module's name.
What is the value of __name__ when a Python script is executed directly from the command line?
When a file is run directly, Python sets __name__ = '__main__'. When imported as a module, __name__ is set to the module's name.
In an interactive Python session, what is the first entry in sys.path used for imports?
In an interactive Python session, sys.path[0] is typically the empty string '', which represents the current working directory.
Which file must be present in a directory to mark it as a Python package in traditional Python (prior to namespace packages)?
__init__.py marks a directory as a regular Python package directory.
What happens when you re-import an already imported module in the same Python session using import mod_name?
Modules are cached in sys.modules. Subsequent imports reuse the cached module object. importlib.reload() is required to re-execute.
Consider a file mod.py containing:
__all__ = ['a', 'b']
$a = 1
b = 2
c = 3
If another script runs$from mod import *, how many variables from mod are imported into the local namespace?
__all__ explicitly exports only ['a', 'b']. Variable 'c' is excluded. Thus 2 variables are imported.