The __all__
property of a module is used to define the list of names that will be imported when performing a wildcard import of this
module, i.e. when from mymodule import *
is used.
In the following example:
# mymodule.py
def foo(): ...
def bar(): ...
__all__ = ["foo"]
Executing from mymodule import *
from a different module will only import foo
.
This list can only reference defined names, otherwise an AttributeError
will be raised when the module is imported.
Code examples
Noncompliant code example
from mymodule import my_func
__all__ = ["unknown_func"] # Noncompliant: "unknown_func" is undefined
Compliant solution
from mymodule import my_func
__all__ = ["my_func"]