Python Modules and Packages 2 — Questions and Answers
Question 1: What does the `__all__` variable in a module control?
- Which names are exported when `from module import *` is used (Correct answer)
- Which functions are automatically called on import
- The order in which module attributes are loaded
- Which names are visible in `dir(module)`
Correct answer: Which names are exported when `from module import *` is used
`__all__` is a list of strings defining the public API exported by `from module import *`.
Question 2: Which file must exist in a directory for Python 2 to treat it as a package?
- __init__.py (Correct answer)
- setup.py
- package.py
- __main__.py
Correct answer: __init__.py
In Python 2, a directory must contain `__init__.py` to be recognized as a package; Python 3 introduced namespace packages that don't require it.
Question 3: What is a 'namespace package' in Python 3?
- A package spread across multiple directories without an `__init__.py` (Correct answer)
- A package that only exports names listed in `__all__`
- A package installed in a virtual environment
- A package with a custom `__namespace__` attribute
Correct answer: A package spread across multiple directories without an `__init__.py`
Namespace packages (PEP 420) allow a single logical package to span multiple directories with no `__init__.py` required.
Question 4: What does `importlib.reload(module)` do?
- Re-executes the module's code in the existing module object (Correct answer)
- Creates a new module object and replaces the old one
- Deletes and reimports the module from scratch
- Clears the module's `__dict__` without re-executing code
Correct answer: Re-executes the module's code in the existing module object
`importlib.reload()` re-runs the module source in the existing module namespace, so existing references to the old object still point to it.
Question 5: What is the purpose of `sys.path`?
- It is a list of directories Python searches when importing modules (Correct answer)
- It stores the path to the Python executable
- It defines the current working directory
- It lists installed packages and their versions
Correct answer: It is a list of directories Python searches when importing modules
`sys.path` is the list of directories (and zip files) Python searches in order when resolving import statements.
Question 6: Which statement correctly imports only the `sqrt` function from the `math` module?
- from math import sqrt (Correct answer)
- import math.sqrt
- import sqrt from math
- include math.sqrt
Correct answer: from math import sqrt
`from math import sqrt` binds the name `sqrt` directly in the current namespace without importing the full module.
Question 7: What value does `__name__` hold when a module is imported (not run directly)?
- The module's fully qualified name (Correct answer)
- '__main__'
- None
- The filename without extension
Correct answer: The module's fully qualified name
When a module is imported, `__name__` is set to the module's fully qualified name (e.g., `'mypackage.mymodule'`), not `'__main__'`.
What does the `__all__` variable in a module control?