Today I Learned 1 min read
TIL - In Python __all__ Controls What Imports * Exposes
Defining __all__ in a module explicitly declares its public API. Without it, from module import * imports everything not starting with _. With it, only the listed names are exported.
# utils.py
__all__ = ["format_date", "parse_date"]
def format_date(d): ...
def parse_date(d): ...
def _internal_helper(): ... # not exportedpython
Even if you never use import *, __all__ serves as documentation - it tells readers and IDEs what the public surface area is.
🐍 Note:
__all__does not block direct imports -from utils import _internal_helperstill works.