Python all and the scope over files - all should be used when importing * in outer files
Many Python modules
__all__ to declare variables or functions that outer files can access. In short, the objects in
__all__ are "public" and others are "private". This article supposes the Python application has util directory and it has example.py and helper.py.
/util -example.py -helper.py
from util.helper import * print(a) # 1 print(b) # moon print(pi) # NameError: name 'pi' is not defined
In helper.py, 3 variables are set but only
b are in
__all__. So outer files can access these but can't read
pi. Actually, example.py can't read
pi and raises NameError exception.
__all__ works properly only in the case that an outer file import
*. If it imports the file name,
__all__ can't prohibit the access from outer files.
from util import helper print(helper.a) # 1 print(helper.b) # moon print(helper.pi) # 3.14
helper.pi is printed. After all,
__all__ is not a bar to protect the data from outer accesses.