Is it a convention to prefix private classes with underscores?

后端 未结 4 1044
小鲜肉
小鲜肉 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 23:08

    You can use a single underscore as the first character in any variable, but it is carries the implied meaning, "Do not use outside of the class/module unless you really know what you're doing" (eg. intended protected/internal) and it will not import if you use from import *.

    Using a double underscore is something you should never do outside of a class as it could mess with name mangling otherwise (and by "could", I mean, "caused me a big headache this past week because I did not realize that it does").

提交回复
热议问题