I've already read Can someone explain __all__ in Python? and I understand that it only affects from ... import *
statements, but I can't figure out a real use case. Why should I repeat exported names in __all__
(DRY!) when I could simply avoid importing those names in __init__
namespace?
Example:
mypackage/__init__.py
from a import A
mypackage/a.py
A = "A"
A1 = "A1"
mypackage/b.py
B = "B"
And then in python:
>>> from mypackage import *
>>> A
'A'
>>>
>>> A1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'A1' is not defined
>>> b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined
As you can see A
is in the namespace but A1
and b
are not. Why should I have to define __all__ = ["A"]
?