โ† All Python Flashcard Decks

Modules and Packages Flashcards

7 cards from real Python practice questions. Tap to flip, then mark Knew It or Still Learning โ€” missed cards come back until you master them.

Read the first 7 Modules and Packages flashcards as text
  1. What does the `__all__` variable in a module control?

    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 *`.

  2. Which file must exist in a directory for Python 2 to treat it as a package?

    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.

  3. What is a 'namespace package' in Python 3?

    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.

  4. What does `importlib.reload(module)` do?

    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.

  5. What is the purpose of `sys.path`?

    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.

  6. Which statement correctly imports only the `sqrt` function from the `math` module?

    Answer: from math import sqrt

    `from math import sqrt` binds the name `sqrt` directly in the current namespace without importing the full module.

  7. What value does `__name__` hold when a module is imported (not run directly)?

    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__'`.

Modules and Packages Flashcards โ€” Python Study Cards with Answers