What is Python's __all__ variable?
Why Interviewers Ask This
This question targets practical, hands-on experience with Python. Interviewers want to see if you've worked with these concepts in real projects, not just read about them. Strong answers include concrete examples.
Answer
The __all__ variable is a list of strings defined at the module level that specifies which names should be exported when from module import * is used. Without __all__, all names not starting with an underscore are exported. Example: __all__ = ["public_func", "PublicClass"] — only these names are exported, hiding implementation details. __all__ also serves as documentation of the module's public API and helps IDEs and documentation generators understand what is public. Best practice: always define __all__ in libraries to prevent accidental exposure of private helpers. Note: __all__ only affects from module import * — explicit imports (from module import private_func) are never restricted by __all__. Used alongside _private naming for comprehensive API control.
Pro Tip
Before answering, structure your response: one-line definition → real-world analogy → concrete example from a project. This makes even complex Python answers easy to follow.