Can someone explain __all__ in Python?

前端 未结 11 2640
一个人的身影
一个人的身影 2020-11-21 13:36

I have been using Python more and more, and I keep seeing the variable __all__ set in different __init__.py files. Can someone explain what this d

11条回答
  •  误落风尘
    2020-11-21 14:03

    It also changes what pydoc will show:

    module1.py

    a = "A"
    b = "B"
    c = "C"
    

    module2.py

    __all__ = ['a', 'b']
    
    a = "A"
    b = "B"
    c = "C"
    

    $ pydoc module1

    Help on module module1:
    
    NAME
        module1
    
    FILE
        module1.py
    
    DATA
        a = 'A'
        b = 'B'
        c = 'C'
    

    $ pydoc module2

    Help on module module2:
    
    NAME
        module2
    
    FILE
        module2.py
    
    DATA
        __all__ = ['a', 'b']
        a = 'A'
        b = 'B'
    

    I declare __all__ in all my modules, as well as underscore internal details, these really help when using things you've never used before in live interpreter sessions.

提交回复
热议问题