search

Home  >  Q&A  >  body text

Why can't Python's __all__ prevent "content that is not exported cannot be accessed"?

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

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

a = 1 
b = 2 
c = 3    # 不希望别人访问 
import base
                                                                                                            
print(base.c)
3

environment

大家讲道理大家讲道理2754 days ago743

reply all(2)I'll reply

  • 过去多啦不再A梦

    过去多啦不再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

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-18 10:52:54

    Nothing is truly private in Python

    reply
    0
  • Cancelreply