Can someone explain __all__ in Python?

前端 未结 11 2421
一个人的身影
一个人的身影 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 13:50

    This is defined in PEP8 here:

    Global Variable Names

    (Let's hope that these variables are meant for use inside one module only.) The conventions are about the same as those for functions.

    Modules that are designed for use via from M import * should use the __all__ mechanism to prevent exporting globals, or use the older convention of prefixing such globals with an underscore (which you might want to do to indicate these globals are "module non-public").

    PEP8 provides coding conventions for the Python code comprising the standard library in the main Python distribution. The more you follow this, closer you are to the original intent.

提交回复
热议问题