Is it a convention to prefix private classes with underscores?

后端 未结 4 1074
小鲜肉
小鲜肉 2021-01-17 22:35

I have seen code in which functions/constants are prefixed with underscores. My understanding is that this indicates that they are not to be used directly. Can I do this wit

4条回答
  •  死守一世寂寞
    2021-01-17 22:53

    Better only use one _. This indicates that a name is private within a module.

    It is not imported with the catch-all from import *, and it has some other features such as "preferred destruction".

    From here:

    If __all__ is not defined, the set of public names includes all names found in the module’s namespace which do not begin with an underscore character ('_').

    From here:

    Starting with version 1.5, Python guarantees that globals whose name begins with a single underscore are deleted from their module before other globals are deleted.

    Double-underscore starting class members are name-mangled.

提交回复
热议问题