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
test.py file changed to
The results are as follows:
Nothing is truly private in Python