问题
I have multiple internal functions in a module. Because the module is getting too large, I want to factor out these internal functions into a separate "utility" module in the same package. The utility module would only contain package-internal functions which should not be considered part of the package's public API. Is there an accepted naming convention for such internal modules?
Intuitively, I'd prefix the module name with an underscore. However, that is the naming convention for C/C++ modules according to PEP8.
Simply prefixing the function names with an underscore (as I did until now) doesn't seem right if the functions are in a separate module (because the underscore in that case refer to module-level privacy).
回答1:
I wouldn't say that a prefixed underscore applies exclusively to C/C++ extension modules. Rather, such a module is not often intended to be used directly, but provides a private implementation for a Python module of the same name (cf. socket
/_socket
). By that logic, I don't see why any module not intended for public use (whether implemented in C or Python) should not use the _
prefix.
回答2:
Not as far as I know, but there are some things seen often:
- Module named
internal
orinternal_xyz
- Subpackage named
internal
containing internal modules - Underscore, even though it's meant for C modules. I, personally, prefer this one for it's shortness and because everyone knows what it means.
来源:https://stackoverflow.com/questions/26278185/naming-convention-for-private-modules