Why can't Python's __all__ prevent "content that is not exported cannot be accessed"?
大家讲道理
大家讲道理 2017-05-18 10:50:54
0
2
765

Actual Phenomenon

  1. Expect __all__ Be able to control module access

  2. According to the community contract, private things start with _, but recently I found that a colleague adjusted the private interface (a module I wrote)

  3. Python is a flexible language, and the unwritten rule is "convention over configuration"

Expected Phenomenon

  1. I searched for information on __all__, and thought it could meet my requirements, but it didn’t (see below)

question

So, __all__ seems to be of no use at all?

Related codes

  • base.py

__all__ = ('a', 'b', )

a = 1 
b = 2 
c = 3    # 不希望别人访问 
  • test.py

import base
                                                                                                            
print(base.c)
  • Output

3

environment

  • Python 2.7

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(2)
过去多啦不再A梦

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
曾经蜡笔没有小新

Nothing is truly private in Python

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template