Expect __all__
Be able to control module access
According to the community contract, private things start with _
, but recently I found that a colleague adjusted the private interface (a module I wrote)
Python is a flexible language, and the unwritten rule is "convention over configuration"
I searched for information on __all__
, and thought it could meet my requirements, but it didn’t (see below)
So, __all__
seems to be of no use at all?
base.py
__all__ = ('a', 'b', )
a = 1
b = 2
c = 3 # 不希望别人访问
test.py
import base
print(base.c)
Output
3
Python 2.7
过去多啦不再A梦2017-05-18 10:52:54
test.py file changed to
from base import *
print a
print b
print c
The results are as follows:
❯ python test.py ⏎
1
2
Traceback (most recent call last):
File "test.py", line 8, in <module>
print c
NameError: name 'c' is not defined