I have been using Python more and more, and I keep seeing the variable __all__
set in different __init__.py
files. Can someone explain what this d
__all__
affects from
statements.
Consider this example:
foo
├── bar.py
└── __init__.py
In foo/__init__.py
:
(Implicit) If we don't define __all__
, then from foo import *
will only import names defined in foo/__init__.py
.
(Explicit) If we define __all__ = []
, then from foo import *
will import nothing.
(Explicit) If we define __all__ = [
, then from foo import *
will only import those names.
Note that in the implicit case, python won't import names starting with _
. However, you can force importing such names using __all__
.
You can view the Python document here.